constructors

default preicate is less #include <iostream> #include <set> int main () { int ary[] = { 5,3,7,5,2,3,7,5,5,4 }; set<int> s1; set<int, greater<int> > s2; for ( int i=0; i<sizeof(ary)/sizeof(int); i++ ) { s1.insert(ary[i]); s2.insert(ary[i]); } set<int>::iterator It = s1.begin(); cout << "s1 : "; while ( It != s1.end() ) cout << *(It++) << " "; cout << endl; It = s2.begin(); cout << "s2 : "; while ( It != s2.end() ) cout << *(It++) << " "; cout << endl; // second form of constructor set<int> s3(ary,ary+3); It = s3.begin(); cout << "s3 : "; while ( It != s3.end() ) cout << *(It++) << " "; cout << endl; // copy constructor (predicate of s1 is important) set<int, less > s4(s1); It = s4.begin(); cout << "s4 : "; while ( It != s4.end() ) cout << *(It++) << " "; cout << endl; return 0; } OUTPUT: // s1 : 2 3 4 5 7 // s2 : 7 5 4 3 2 // s3 : 3 5 7 // s4 : 2 3 4 5 7 begin
returns an iterator to the first element #include <iostream> #include <set> using namespace std; int main () { int ary[] = {1,2,3,2,4,5,7,2,6,8}; set<int> s(ary,ary+10); copy(s.begin(),s.end(), ostream_iterator<int>(cout," ")); return 0; } OUTPUT: // 1 2 3 4 5 6 7 8 clear
removes all elements #include <iostream> #include <set> using namespace std; void print (set<int, less<int> >& s) { set<int, less<int> >::iterator It; for ( It = s.begin(); It != s.end(); It++ ) cout << *It << " "; cout << endl; } //-------------------------------------------- int main () { int ary[] = {1,2,3,2,3,4,8,2,5,6}; set<int, less<int> > s; s.insert(ary,ary+10); print(s); s.clear(); cout << "Size of set s = " << s.size() << endl; print(s); return 0; } OUTPUT: // 1 2 3 4 5 6 8 // Size of set s = 0 count
returns the number of elements #include <iostream> #include <set> using namespace std; void print (set<int, less<int> >& s) { set<int, less<int> >::iterator It; for ( It = s.begin(); It != s.end(); It++ ) cout << *It << " "; cout << endl; } //-------------------------------------------- int main () { int ary[] = {1,2,3,2,3,4,8,2,5,6}; set<int, less<int> > s; s.insert(ary,ary+10); print(s); cout << "count of '2' (0 or 1) is "; int n = s.count(2); cout << n << endl; return 0; } OUTPUT: // 1 2 3 4 5 6 8 // count of '2' (0 or 1) is 1 empty
true if the set is empty #include <iostream> #include <set> using namespace std; void print (set<int, less<int> >& s) { set<int, less<int> >::iterator It; for ( It = s.begin(); It != s.end(); It++ ) cout << *It << " "; cout << endl; } //-------------------------------------------- int main () { int ary[] = {1,2,3,2,3,4,8,2,5,6}; set<int, less<int> > s; s.insert(ary,ary+10); print(s); cout << "set is " << ((s.empty()) ? "" : "not ") << "empty" << endl; s.clear(); cout << "set is " << ((s.empty()) ? "" : "not ") << "empty" << endl; return 0; } OUTPUT: // 1 2 3 4 5 6 8 // set is not empty // set is empty end
returns an iterator to the last element #include <iostream> #include <set> #include <iomanip> #include <string> using namespace std; template <class T> class Member { public: Member(T l, T f) : last(l), first(f) {} void print() const // const !!! { cout.setf(ios::left); cout << setw(15) << first.c_str() << last << endl; } private: T first, last; // const !!! friend bool operator < (const Member& m1, const Member& m2) { return (m1.last < m2.last) ? true : false; } friend bool operator == (const Member& m1, const Member& m2) { return (m1.last == m2.last) ? true : false; } }; //=============================================== int main () { typedef Member<string> M; typedef set<M, less<M> > S; M m("Frost","Robert"); S s; s.insert(m); s.insert(M("Smith","John")); s.insert(M("Amstrong","Bill")); s.insert(M("Bain","Linda")); S::iterator It = s.begin(); while ( It != s.end() ) (It++)->print(); return 0; } OUTPUT: // Bill Amstrong // Linda Bain // Robert Frost // John Smith equal_ranges
returns iterators to the first and last elements that match a certain key #include <iostream> #include <set> using namespace std; int main () { set<int> c; c.insert(1); c.insert(2); c.insert(4); c.insert(10); c.insert(11); cout << "lower_bound(3): " << *c.lower_bound(3) << endl; cout << "upper_bound(3): " << *c.upper_bound(3) << endl; cout << "equal_range(3): " << *c.equal_range(3).first << " " << *c.equal_range(3).second << endl; cout << endl; cout << "lower_bound(5): " << *c.lower_bound(5) << endl; cout << "upper_bound(5): " << *c.upper_bound(5) << endl; cout << "equal_range(5): " << *c.equal_range(5).first << " " << *c.equal_range(5).second << endl; cin.get(); } OUTPUT: // lower_bound(3): 4 // upper_bound(3): 4 // equal_range(3): 4 4 // // lower_bound(5): 10 // upper_bound(5): 10 // equal_range(5): 10 10 erase
removes elements #include <iostream> #include <set> using namespace std; void print (set<int, less<int> >& s) { set<int, less<int> >::iterator It; for ( It = s.begin(); It != s.end(); It++ ) cout << *It << " "; cout << endl; } //-------------------------------------------- int main () { int ary[] = {1,2,3,2,3,4,8,2,5,6}; set<int, less<int> > s; s.insert(ary,ary+10); print(s); // erase '2' s.erase(2); print(s); set<int, less<int> >::iterator It; It = s.find(5); // erase '5' s.erase(It); print(s); It = s.find(4); // erase from It to the end of set s.erase(It,s.end()); print(s); return 0; } OUTPUT: // 1 2 3 4 5 6 8 // 1 3 4 5 6 8 // 1 3 4 6 8 // 1 3 find
finds a given element #include <iostream> #include <set> #include <iomanip> #include <string> using namespace std; template <class T> class Member { public: Member(T l, T f) : last(l), first(f) {} void print() const // const !!! { cout.setf(ios::left); cout << setw(15) << first.c_str() << last << endl; } private: T first, last; // const !!! friend bool operator < (const Member& m1, const Member& m2) { return (m1.last < m2.last) ? true : false; } friend bool operator == (const Member& m1, const Member& m2) { return (m1.last == m2.last) ? true : false; } }; //=============================================== int main () { typedef Member<string> M; typedef set<M, less<M> > S; M m("Frost","Robert"); S s; s.insert(m); s.insert(M("Smith","John")); s.insert(M("Amstrong","Bill")); s.insert(M("Bain","Linda")); S::iterator It = s.begin(); while ( It != s.end() ) (It++)->print(); It = s.find(m); if ( It == s.end() ) cout << "element not found" << endl; else { cout << "element is found : "; (*It).print(); } return 0; } OUTPUT: // Bill Amstrong // Linda Bain // Robert Frost // John Smith // element is found : Robert Frost insert
inserts elements into the set #include <iostream> #include <set> using namespace std; void print (set<int, less<int> >& s) { set<int, less<int> >::iterator It; for ( It = s.begin(); It != s.end(); It++ ) cout << *It << " "; cout << endl; } //-------------------------------------------- int main () { int ary[] = {1,2,3,2,3,4,8,2,5,6}; set<int, less<int> > s; s.insert(10); print(s); s.insert(ary,ary+5); print(s); set<int, less<int> >::iterator It = s.begin(); s.insert(It,20); print(s); return 0; } OUTPUT: // 10 // 1 2 3 10 // 1 2 3 10 20 lower_bound
returns an iterator to the first element greater than a certain value #include <iostream> #include <set> #include <iomanip> #include <string> using namespace std; template <class T> class Member { public: Member(T l) : last(l), first("") {} // for upper_bound // and lower_bound Member(T l, T f) : last(l), first(f) {} void print() const // const !!! { cout.setf(ios::left); cout << setw(15) << first.c_str() << last << endl; } private: T first, last; // const !!! friend bool operator < (const Member& m1, const Member& m2) { return (m1.last < m2.last) ? true : false; } friend bool operator == (const Member& m1, const Member& m2) { return (m1.last == m2.last) ? true : false; } }; //=============================================== int main () { typedef Member<string> M; typedef set<M, less<M> > S; S s; s.insert(M("Smith","John")); s.insert(M("Shevchenko","Taras")); s.insert(M("Amstrong","Bill")); s.insert(M("Bain","Linda")); s.insert(M("Pushkin","Alexander")); s.insert(M("Pasternak","Biris")); S::iterator It = s.begin(); while ( It != s.end() ) (It++)->print(); cout << endl; M m1("P"); M m2("Pzz"); S::iterator low = s.lower_bound(m1); S::iterator upp = s.upper_bound(m2); It = low; while ( It != upp ) (It++)->print(); return 0; } OUTPUT: // Bill Amstrong // Linda Bain // Biris Pasternak // Alexander Pushkin // Taras Shevchenko // John Smith // // Biris Pasternak // Alexander Pushkin key_comp
returns the function that compares keys #include <iostream> #include <set> using namespace std ; template void truefalse(T t) { cout << (t?"True":"False") << endl; } int main () { set<int> s; cout << "s.key_comp()(1,2) returned "; truefalse(s.key_comp()(1,2)); // True cout << "s.key_comp()(2,1) returned "; truefalse(s.key_comp()(2,1)); // False cout << "s.key_comp()(1,1) returned "; truefalse(s.key_comp()(1,1)); // False return 0; } OUTPUT: // s.key_comp()(1,2) returned True // s.key_comp()(2,1) returned False // s.key_comp()(1,1) returned False max_size
the maximum number of elements that the set can hold #include <iostream> #include <set> #include <algorithm> using namespace std; void print (set<int, less<int> >& s) { copy(s.begin(),s.end(), ostream_iterator<int>(cout," ")); cout << endl; } //-------------------------------------------- int main () { int ary[] = {1,2,3,2,3,4,8,2,5,6}; set<int, less<int> > s; s.insert(ary,ary+10); print(s); cout << "size of set 's' = " << s.size() << endl; cout << "max_size of 's' = " << s.max_size() << endl; return 0; } OUTPUT: // 1 2 3 4 5 6 8 // size of set 's' = 7 // max_size of 's' = 4294967295 rbegin
returns a reverse iterator to the end of the set #include <iostream> #include <set> #include <iomanip> #include <string> using namespace std; template <class T> class Member { public: Member(T l, T f) : last(l), first(f) {} void print() const // const !!! { cout.setf(ios::left); cout << setw(15) << first.c_str() << last << endl; } private: T first, last; // const !!! friend bool operator < (const Member& m1, const Member& m2) { return (m1.last < m2.last) ? true : false; } friend bool operator == (const Member& m1, const Member& m2) { return (m1.last == m2.last) ? true : false; } }; //=============================================== int main () { typedef Member<string> M; typedef set<M, less<M> > S; M m("Frost","Robert"); S s; s.insert(m); s.insert(M("Smith","John")); s.insert(M("Amstrong","Bill")); s.insert(M("Bain","Linda")); S::iterator It = s.begin(); while ( It != s.end() ) (It++)->print(); cout << endl; S::reverse_iterator rI = s.rbegin(); while ( rI != s.rend() ) (rI++)->print(); return 0; } OUTPUT: // Bill Amstrong // Linda Bain // Robert Frost // John Smith // // John Smith // Robert Frost // Linda Bain // Bill Amstrong rend
returns a reverse iterator to the beginning of the set #include <iostream> #include <set> #include <iomanip> #include <string> using namespace std; template <class T> class Member { public: Member(T l, T f) : last(l), first(f) {} void print() const // const !!! { cout.setf(ios::left); cout << setw(15) << first.c_str() << last << endl; } private: T first, last; // const !!! friend bool operator < (const Member& m1, const Member& m2) { return (m1.last < m2.last) ? true : false; } friend bool operator == (const Member& m1, const Member& m2) { return (m1.last == m2.last) ? true : false; } }; //=============================================== int main () { typedef Member<string> M; typedef set<M, less<M> > S; M m("Frost","Robert"); S s; s.insert(m); s.insert(M("Smith","John")); s.insert(M("Amstrong","Bill")); s.insert(M("Bain","Linda")); S::iterator It = s.begin(); while ( It != s.end() ) (It++)->print(); cout << endl; S::reverse_iterator rI = s.rbegin(); while ( rI != s.rend() ) (rI++)->print(); return 0; } OUTPUT: // Bill Amstrong // Linda Bain // Robert Frost // John Smith // // John Smith // Robert Frost // Linda Bain // Bill Amstrong size
the number of elements in the set #include <iostream> #include <set> using namespace std; void print (set<int, less<int> >& s) { set<int, less<int> >::iterator It; for ( It = s.begin(); It != s.end(); It++ ) cout << *It << " "; cout << endl; } //-------------------------------------------- int main () { int ary[] = {1,2,3,2,3,4,8,2,5,6}; set<int, less<int> > s; s.insert(ary,ary+10); cout << "Size of set s = " << s.size() << endl; print(s); s.clear(); cout << "Size of set s = " << s.size() << endl; return 0; } OUTPUT: // Size of set s = 7 // 1 2 3 4 5 6 8 // Size of set s = 0 swap
exchanges two sets #include <iostream> #include <set> #include <algorithm> using namespace std; void print (set<int, less<int> >& s) { copy(s.begin(),s.end(), ostream_iterator<int>(cout," ")); cout << endl; } //-------------------------------------------- int main () { int ary1[] = {1,2,3,2,3,4,8,2,5,6}; int ary2[] = {5,0,9,2,3,4,8,2,5,6}; set<int, less<int> > s1, s2; s1.insert(ary1,ary1+10); cout << "s1 : "; print(s1); cout << "s2 : "; s2.insert(ary2,ary2+10); print(s2); if ( s1 != s2 ) s1.swap(s2); cout << "s1 : "; print(s1); cout << "s2 : "; print(s2); return 0; } OUTPUT: // s1 : 1 2 3 4 5 6 8 // s2 : 0 2 3 4 5 6 8 9 // s1 : 0 2 3 4 5 6 8 9 // s2 : 1 2 3 4 5 6 8 upper_bound
returns an iterator to the first element greater than a certain value #include <iostream> #include <set> #include <iomanip> #include <string> using namespace std; template <class T> class Member { public: Member(T l) : last(l), first("") {} // for upper_bound // and lower_bound Member(T l, T f) : last(l), first(f) {} void print() const // const !!! { cout.setf(ios::left); cout << setw(15) << first.c_str() << last << endl; } private: T first, last; // const !!! friend bool operator < (const Member& m1, const Member& m2) { return (m1.last < m2.last) ? true : false; } friend bool operator == (const Member& m1, const Member& m2) { return (m1.last == m2.last) ? true : false; } }; //=============================================== int main () { typedef Member<string> M; typedef set<M, less<M> > S; S s; s.insert(M("Smith","John")); s.insert(M("Shevchenko","Taras")); s.insert(M("Amstrong","Bill")); s.insert(M("Bain","Linda")); s.insert(M("Pushkin","Alexander")); s.insert(M("Pasternak","Boris")); S::iterator It = s.begin(); while ( It != s.end() ) (It++)->print(); cout << endl; M m1("P"); M m2("Pzz"); S::iterator low = s.lower_bound(m1); S::iterator upp = s.upper_bound(m2); It = low; while ( It != upp ) (It++)->print(); return 0; } OUTPUT: // Bill Amstrong // Linda Bain // Biris Pasternak // Alexander Pushkin // Taras Shevchenko // John Smith // // Boris Pasternak // Alexander Pushkin value_comp
returns the function that compares values #include <iostream> #include <set> using namespace std ; template void truefalse(T t) { cout << (t?"True":"False") << endl; } int main () { set<int> s; cout << "s.value_comp()(1,2) returned "; truefalse(s.value_comp()(1,2)); // True cout << "s.value_comp()(2,1) returned "; truefalse(s.value_comp()(2,1)); // False cout << "s.value_comp()(1,1) returned "; truefalse(s.value_comp()(1,1)); // False return 0; } OUTPUT: // s.value_comp()(1,2) returned True // s.value_comp()(2,1) returned False // s.value_comp()(1,1) returned False