首页 > 解决方案 > 按第二个元素对c ++中的一对向量进行排序,如果第二个相同,则根据第一个对其进行排序

问题描述

谁能解释一下它是如何工作的?

我们没有调用sortbysec函数,但我仍然得到了正确的答案。为什么不需要传递对sortbysec

请解释一下,因为我对std::sort功能的内部运作感到困惑。

bool sortbysec(const pair<int, int> &a, const pair<int, int> &b)
{
    if (a.second == b.second)
        return (a.first < b.first);
    else
        return (a.second < b.second);
}

sort(v.begin(), v.end(), sortbysec);

标签: c++sortingvectorstdstd-pair

解决方案


推荐阅读