# Include <set>
# Include <iterator>
# Include <iostream>
# Include <algorithm>
Using namespace STD;
Int main ()
{
Set <int> eg1;
Eg1.insert (1 );
Eg1.insert (100 );
Eg1.insert (5 );
Eg1.insert (1); // element 1 does not insert 1 again in the set because it already exists.
Eg1.insert (10 );
Eg1.insert (9 );
// Traverse the set and we can find that the elements are ordered.
Set <int>: iterator set_iter = eg1.begin ();
Cout <"set named eg1:" <Endl;
For (; set_iter! = Eg1.end (); set_iter ++) cout <* set_iter <"";
Cout <Endl;
// Use the size () function to obtain the number of current elements
Cout <"Now there are" <eg1.size () <"elements in the Set eg1" <Endl;
If (eg1.find (200) = eg1.end () // The find () function can find whether an element exists.
Cout <"200 isn' t in the Set eg1" <Endl;
set eg2;
for (INT I = 6; I <15; I ++)
eg2.insert (I );
cout <"set named eg2:" for (set_iter = eg2.begin (); set_iter! = Eg2.end (); set_iter ++)
cout <* set_iter <"";
cout
// obtain the union of two sets
set eg3;
cout <"Union (Union of two sets ):";
set_union (eg1.begin (),
eg1.end (),
eg2.begin (),
eg2.end (),
insert_iterator (eg3, eg3.begin ()
); // pay attention to the form of the fifth parameter
copy (eg3.begin (), eg3.end (), ostream_iterator (cout, "");
cout
// obtain the intersection of two sets. Note that the set that receives the result before performing the set operation must call the clear () function to clear the result.
eg3.clear ();
set_intersection (eg1.begin (),
eg1.end (),
eg2.begin (),
eg2.end (),
insert_iterator (eg3, eg3.begin ()
);
cout <"intersection :";
copy (eg3.begin (), eg3.end (), ostream_iterator (cout, "");
cout
// Obtain the difference between two sets
Eg3.clear ();
Set_difference (eg1.begin (),
Eg1.end (), eg2.begin (),
Eg2.end (),
Insert_iterator <set <int> (eg3, eg3.begin ())
);
Cout <"difference :";
Copy (eg3.begin (), eg3.end (), ostream_iterator <int> (cout ,""));
Cout <Endl;
// Obtain the symmetry difference between two sets, that is, if the two sets are A and B respectively, then the symmetry difference is AUB-A ∩ B
Eg3.clear ();
Set_effecric_difference (eg1.begin (), eg1.end (), eg2.begin (), eg2.end (), insert_iterator <set <int> (eg3, eg3.begin ()));
Copy (eg3.begin (), eg3.end (), ostream_iterator <int> (cout ,""));
Cout <Endl;
Return 0;
}
The following is an example where the keyword type is char *.Code
# Include <iostream>
# Include <iterator>
# Include <set>
Using namespace STD;
Struct ltstr
{
Bool operator () (const char * S1, const char * S2) const
{
Return strcmp (S1, S2) <0;
}
};
Int main ()
{
Const int n = 6;
Const char * A [n] = {"isomer", "Ephemeral", "prosaic ",
"Nugatory", "artichoke", "serif "};
Const char * B [N] = {"flat", "this", "artichoke ",
"Frigate", "prosaic", "isomer "};
Set <const char *, ltstr> A (A, A + n );
Set <const char *, ltstr> B (B, B + n );
Set <const char *, ltstr> C;
Cout <"Set :";
// Copy (A. Begin (), A. End (), ostream_iterator <const char *> (cout ,""));
Set <const char *, ltstr >:: iterator itr;
For (itr = A. Begin (); itr! = A. End (); itr ++) cout <* itr <"";
Cout <Endl;
Cout <"Set B :";
Copy (B. Begin (), B. End (), ostream_iterator <const char *> (cout ,""));
Cout <Endl;
Cout <"union :";
Set_union (A. Begin (), A. End (), B. Begin (), B. End (),
Ostream_iterator <const char *> (cout ,""),
Ltstr ());
Cout <Endl;
Cout <"intersection :";
Set_intersection (. begin (),. end (), B. begin (), B. end (), ostream_iterator <const char *> (cout, ""), ltstr ());
Cout <Endl;
Set_difference (. begin (),. end (), B. begin (), B. end (), inserter (C, C. begin (), ltstr ());
Cout <"set C (difference of A and B ):";
Copy (C. Begin (), C. End (), ostream_iterator <const char *> (cout ,""));
Cout <Endl;
Return 0;
}
The ltstr can also be defined as follows:
Class ltstr
{
Public:
Bool operator () (const char * S1, const char * S2) const
{
Return strcmp (S1, S2) <0;
}
};
The more common application method is that the data type is also replaced by a user-defined class. Compared functions can be customized, or even two-level comparison can be added. For example, the data is sorted by the total score first, if the scores are the same, sort by ID. The following is an example code.
# Include <set>
# Include <iostream>
Using namespace STD;
Struct
{
Int ID;
Int score;
String name;
};
Struct compare
{
Bool operator () (const entity & E1, const entity & E2) const {
If (e1.score <e2.score) return true;
Else
If (e1.score = e2.score)
If (e1.id <e2.id) return true;
Return false;
}
};
Int main ()
{
Set <entity, compare> s_test;
Entity A, B, C;
A. ID = 123; A. Score = 90; A. Name = "bill ";
B. ID = 121; B. Score = 85; B. Name = "Mary ";
C. ID = 130; C. Score = 85; C. Name = "Jerry ";
S_test.insert (a); s_test.insert (B); s_test.insert (C );
Set <entity, compare >:: iterator itr;
Cout <"Score list (ordered by score): \ n ";
For (itr = s_test.begin (); itr! = S_test.end (); itr ++)
Cout <itr-> id <"---" <itr-> name <"---" <itr-> score <Endl;
Return 0;
}