Set and Multiset will automatically sort the elements according to specific sorting rules. The difference between the two lies in that Multiset allows repeated elements while set does not.
Before using set or Multiset, you must add the header file <set>
Set and Multiset are usually completed by the red and black trees. The red and black trees are excellent in changing the number of elements and searching for elements. It ensures that only two reconnection actions can be performed during node security, in addition, the maximum path depth to an element is only twice the minimum path depth.
Automatic Sorting leads to an important restriction on set and Multiset: you cannot directly change the element value, which will disrupt the original correct order. Therefore, to change the element value, you must first Delete the old element and then insert the new element.
There are two ways to define the sorting criterion:
1. Define with template parameter
2. defined by constructors
Search operation functions of set and Multiset:
Count (ELEM) returns the number of elements whose element value is ELEM.
Find (ELEM) returns the first element whose element value is ELEM. If it cannot be found, end () is returned ()
Lower_bound (ELEM) returns the first pluggable location of ELEM, that is, the first element location where the element value> = ELEM
Upper_bound (ELEM) returns the last pluggable location of ELEM, that is, the first element location of the element value> ELEM.
When _range (ELEM) returns the first and last positions that can be inserted by ELEM, that is, the element range where the element value = ELEM
int main(){ set<int>c; c.insert(1); c.insert(2); c.insert(3); c.insert(4); c.insert(5); c.insert(6); cout << *c.lower_bound(3) << endl; cout << *c.upper_bound(3) << endl; cout << *c.equal_range(3).first << " " << *c.equal_range(3).second << endl;}
Output: 3 4 3 4
[STL Study Notes] Set, Multiset