1 #include<iostream> 2 #include<set> 3 using namespace std; 4 struct dian{ 5 int x,y; 6 /*bool operator<(const dian &a) const 7 { 8 return x<a.x; 9 }*/ 10 }; 11 /* 自定义排序函数一定要用const*/ 12 bool operator<(const dian &a,const dian &b) 13 { 14 return a.x<b.x; 15 } 16 void swap(set<dian> &s1,set<dian> &s2) 17 { 18 set<dian> s; 19 s=s1; 20 s1=s2; 21 s2=s; 22 } 23 int main() 24 { 25 set<dian> s1,s2; 26 dian a; 27 a.x=6; 28 a.y=7; 29 dian b; 30 b.x=7; 31 b.y=9; 32 s1.insert(a); 33 s1.insert(b); 34 // s1.swap(s2); 35 // swap(s2,s1); 36 s2=s1; 37 set<dian> s(s2); 38 for(auto i:s) 39 { 40 cout<<i.x<<" "<<i.y<<endl; 41 } 42 }
同set一样,这些带排序效果的容器,自定义排序函数时都需要加上“const”