我想重载2对之间的运算符<。当我使用<进行比较时,它可以工作,但是当我使用内置函数cpp中的sort_in时,它就无法工作。#include使用命名空间std;
typedef pair<int,int> pii;
bool operator < (const pii &a,const pii &b){
return a.second<b.second;
}
int main()
{
pii a,b;
a=make_pair(1,4);
b=make_pair(2,3);
if(a<b) cout<<"a<b\n";
else cout<<"b<a\n";
vector<pii> v;
v.push_back(a);
v.push_back(b);
sort(v.begin(),v.end());
for(auto x:v)
cerr<<x.first<<" "<<x.second<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
b<a
1 4
2 3
Run Code Online (Sandbox Code Playgroud)
我很困惑为什么它不打印:
b<a
2 3
1 4
Run Code Online (Sandbox Code Playgroud)