I. concept
set is a standard association container in STL. It uses a balanced search tree - red black tree to implement the underlying layer. When inserting and deleting operations, only pointer operation nodes are needed to complete, which does not involve memory movement and copy, so the efficiency is relatively high.
Set, as the name implies, means "set". In a set, the elements are unique. By default, the elements are automatically arranged in ascending order. It supports some operations on the set, such as set intersection, set difference and so on. If it is necessary to allow the elements in the set to be repeated, multiset can be used.
- Operations such as union, intersection, difference, symmetry difference, etc. will not be discussed in detail for the time being. The header file "algorithm" should be included in use.
- Unordered set and unordered multiset are unordered versions of set and multiset. The header file "unordered set" should be included in use
2, Common operations
Pay attention to include header file < set > STD:: set and STD:: multiset associative containers when using s.begin() returns the first element of the set container s.end() returns the last element of the set container s.clear() deletes all elements in the set container s.empty() to determine whether the set container is empty s.insert() inserts an element s.erase() removes an element s.size() returns the number of elements in the current set container
2.1 template prototype
template <class Key, class Compare=less<Key>, class Alloc=STL_DEFAULT_ALLOCATOR(Key) >
2.2 statement
#include <iostream> #include <set> #include <functional> using namespace std; set<int> s; int main(){ set<int > seta; //The default is set smaller than the comparator less < int > set<int, greater<int> > setb; //To create a set with a larger comparer, you need to include the header file functional int a[5] = {1,2,3,4,5}; set<int > setc(a,a+5); //Array a initializes a set; set<int > setd(setc.begin(),setc.end()); //setc initializes a set //The above two examples are interval initialization set<int > sete(setd); //Copy construct create set return 0; }
2.3 insertion
#include <iostream> #include <set> using namespace std; set<int >s; void setprint(int cnt){ cout << "Test output :" << cnt << ":" << endl; for(set<int>::iterator it = s.begin(); it!= s.end(); it++) cout << *it << " "; puts(""); return ; } int main(){ int cnt = 1; s.insert(1); s.insert(2); s.insert(5); setprint(cnt++); s.insert(2); //set can only appear once with one value. To insert the same element, use multiset setprint(cnt++); int a[4] = {11,12,13,14}; s.insert(a,a+4); //Insert the elements in the interval [a, a+4] into the container setprint(cnt++); return 0; }
2.4 delete
s.erase() removes an element s.clear() deletes all elements in the set container
#include <iostream> #include <set> using namespace std; set<int >s; void setprint(int cnt){ cout << "Test output :" << cnt << ":" << endl; for(set<int>::iterator it = s.begin(); it!= s.end(); it++) cout << *it << " "; puts(""); return ; } int main(){ int cnt = 1; for(int i = 1; i < 11; i++){ s.insert(i); } setprint(cnt++); s.erase(9); //Delete by element setprint(cnt++); set<int>::iterator ita = s.begin(); set<int>::iterator itb = s.begin(); s.erase(ita); //Remove the element that the iterator points to setprint(cnt++); ita = s.begin(); itb = s.begin(); itb++;itb++; s.erase(ita,itb); //Delete element of interval [ita,itb) setprint(cnt); s.clear(); return 0; }
2.5 amendment
s.find() looks up an element, if it does not exist in the container, the return value is equal to s.end()
#include <iostream> #include <set> using namespace std; set<int >s; void setprint(int cnt){ cout << "Test output :" << cnt << ":" << endl; for(set<int>::iterator it = s.begin(); it!= s.end(); it++) cout << *it << " "; puts(""); return ; } int main(){ int cnt = 1; s.insert(1); s.insert(2); s.insert(5); setprint(cnt++); if(s.find(2) != s.end()) cout << "2 is existent" << endl; else cout << "2 is non-existent" << endl; if(s.find(3) == s.end()) cout << "3 is non-existent" << endl; else cout << "2 is existent" << endl; return 0; }
2.6 empty or judge whether the element is in set
#include <iostream> #include <set> #include <functional> using namespace std; int main(){ set<int > s; if(s.empty()) cout << "Empty container" << endl; s.insert(1); if(!s.empty()) cout << "Container is not empty" << endl; if(s.count(1)) cout << "1 In containers" << endl; if(!s.count(2)) cout << "2 Not in container" << endl; return 0; }
2.7 custom comparison function
#include <iostream> #include <set> #include <functional> using namespace std; struct cmp{ bool operator () (const int &a, const int &b){ return a > b; } }; set<int, cmp>s; //Custom sorting function construction set void setprint(int cnt){ cout << "Test output :" << cnt << ":" << endl; for(set<int,cmp>::iterator it = s.begin(); it!= s.end(); it++) cout << *it << " "; puts(""); return ; } int main(){ s.insert(1); s.insert(2); s.insert(6); setprint(1); return 0; }
2.8 other common operations
s.lower_bound() returns the first element greater than or equal to the given key value s.upper_bound() returns the first element greater than the given key value s. Equal range() returns a pair of locators, representing the first element greater than or equal to the given key value and the first element greater than the given key value respectively The return value is a pair type. If any of the pair of locators fails to return, it will be equal to s.end()
#include <iostream> #include <set> using namespace std; int main(){ set s; s.insert(1); s.insert(2); s.insert(5); cout << "lower_bound & upper_bound test:" << endl; cout << "First element greater than or equal to 3: " << *s.lower_bound(3) << endl; cout << "First element greater than or equal to 2: " <<*s.lower_bound(2) << endl; cout << "First element greater than 2: " <<*s.upper_bound(2) << endl; cout << "equal_range test:" << endl; cout << "First element greater than or equal to 2: " << *s.equal_range(2).first << endl; cout << "First element greater than 2: " << *s.equal_range(2).second << endl; return 0; }