Set: After inserting automatically in order from small to large, if the structure of the need to overload <, the elements in the collection is not allowed to repeat.
Some of the operations of set are shown in code:
#include <iostream>#include<Set>using namespaceStd;typedefstructtagstudentinfo{intNID; stringStrName; BOOL operator< (tagstudentinfoConst& _a)Const//Ascending order { if(nid<_a.nid)return true; if(NID = =_a.nid)returnStrname.compare (_a.strname) <0; return false; }}sinfo,*PsInfo;intMain () {Set<sInfo>sets; Sinfo Stuinfo; Stuinfo.nid=Ten; Stuinfo.strname="test1"; Sets.insert (Stuinfo); Stuinfo.nid= -; Stuinfo.strname="test2"; Sets.insert (Stuinfo); Set<sInfo>:: Iterator sIter; for(Siter=sets.begin (); Siter!=sets.end (); siter++) {cout<< (*siter). NID <<" "<< (*siter). StrName <<Endl; } Set<int>A; Set<int>b; Set<int>B; Set<int>D; Set<int>e; A.insert (1); A.insert (2); A.insert (3); A.insert (4); A.insert (5); B.insert (3); B.insert (4); B.insert (5); Set_union (A.begin (), A.end (), B.begin (), B.end (), Inserter (c, C.begin ())); Set<int>:: iterator S; for(S=c.begin (); S!=c.end (); s++) {cout<< (*s) <<","; } cout<<Endl; Set_intersection (A.begin (), A.end (), B.begin (), B.end (), Inserter (d, D.begin ())); for(S=d.begin (); S!=d.end (); s++) {cout<< (*s) <<","; } cout<<Endl; Set_difference (A.begin (), A.end (), B.begin (), B.end (), Inserter (E, E.begin ())); for(S=e.begin (); S!=e.end (); s++) {cout<< (*s) <<","; } cout<<Endl; return 0;}
STL Learning Notes