65,211
社区成员
发帖
与我相关
我的任务
分享
//UP
#include "iostream"
#include "vector"
#include "algorithm"
using namespace std;
struct st
{
int a;
int b;
st(int _a = 0, int _b = 0) : a(_a), b(_b) {}
st(const st& other) : a(other.a), b(other.b) {}
bool operator < (const st& other)
{
if (a < other.a)
return true;
if (a == other.a)
return b < other.b;
return false;
}
};
int main()
{
vector<st> vst;
vst.push_back(st(1, 2));
vst.push_back(st(3, 2));
vst.push_back(st(1, 3));
vst.push_back(st(5, 7));
vst.push_back(st(7, 4));
vst.push_back(st(2, 3));
vst.push_back(st(2, 8));
vst.push_back(st(8, 1));
for (size_t i = 0; i < vst.size(); ++i)
cout<<"("<<vst[i].a<<", "<<vst[i].b<<")"<<" ";
cout<<endl;
sort(vst.begin(), vst.end());
cout<<"after sort : "<<endl;
for (size_t i = 0; i < vst.size(); ++i)
cout<<"("<<vst[i].a<<", "<<vst[i].b<<")"<<" ";
cout<<endl;
return 0;
}
-----------
(1, 2) (3, 2) (1, 3) (5, 7) (7, 4) (2, 3) (2, 8) (8, 1)
after sort :
(1, 2) (1, 3) (2, 3) (2, 8) (3, 2) (5, 7) (7, 4) (8, 1)