set的交換,賦值以及初始化


 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”


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM