C++ 使用STL C+对向量对进行排序+;

C++ 使用STL C+对向量对进行排序+;,c++,C++,我想根据first的值按降序对向量对进行排序。如果first的值相同,我想根据second的值按升序对其进行排序。STL中有什么方法可以做到这一点吗? 假设这是我的向量对- (3,u) (1,d) (3,t) 如果我用这个- vector < pair <int ,char > >M1(3); sort(M1.rbegin(),M1.rend()); 但这正是我想要的- (3,t) (3,u) (1,d) 是的,您所需要做的就是提供一个比较器(或者重写运算符,就像您

我想根据first的值按降序对向量对进行排序。如果first的值相同,我想根据second的值按升序对其进行排序。STL中有什么方法可以做到这一点吗? 假设这是我的向量对-

(3,u)
(1,d)
(3,t)
如果我用这个-

vector < pair <int ,char > >M1(3);
sort(M1.rbegin(),M1.rend());
但这正是我想要的-

(3,t)
(3,u)
(1,d)

是的,您所需要做的就是提供一个比较器(或者重写
运算符,就像您希望对这些对进行自定义比较一样。您的条件是错误的(当
(p2.first
结果在所有情况下都应为false)。OP希望
返回std::tie(p2.first,p1.second)
重写
操作符@Nitori我也收到了这个错误-
候选对象需要2个参数,3个提供的排序(M1.begin(),M1.end(),customComparison);
啊,我知道了,你说得对,Jarod。我已经更新了逻辑。我还修复了关于参数计数的问题(模板没有像我希望的那样自动推断)。
(3,t)
(3,u)
(1,d)
template<typename T, typename U>
bool customComparison(const std::pair<T, U> &p1, const std::pair<T, U> &p2)
{
    return std::tie(p2.first, p1.second) < std::tie(p1.first, p2.second);
}

sort(vec.begin(), vec.end(), customComparison<int,char>);