Set

#include <set>  

set<int> s;                             // create a set of integers
set<int> s({4, 3, 9, 2, 0, 6});         // create a set with few initial elements 


cout << s.size() << endl;               // number of elements in set
s.insert(123);                          // add element to set
s.erase(444);                           // erase an element from the set
s.clear();                              // remove all the elements from the set

// search for an element
if (s.find(444) != s.end()) {
    // It means 444 exists in the set
}

// iterate over all elements
for(set<int>::iterator it = s.begin(); it != s.end(); it++) {
    cout << *it << endl;
}
  

Last updated