Summary of set/multiset practices in STL
(1) Before using set/multiset, you must include the header file <set >:# include <set>
(2) namespace std {
Template <class T, class Compare = less <T>,
Class Allocator = allocator <T>
Class set;
Template <class T, class Compare = less <T>,
Class Allocator = allocator <T>
Class multiset;
}
Any assignable, copyable, or comparable type T can become the element type of set or multiset.
The set/multiset sorting criterion must be strict weak ordering. The meaning is: 1 must be called back. For the sort op, if op (x, y) is true, op (y, x) is false. 2 must be passed. If op (x, y) is true and op (y, z) is true, op (x, z ). 3. The op (x, x) is always false.
(3) set/multiset is usually implemented by a balanced binary tree. In fact, most of the implementation versions of set/multiset are implemented by the red/black tree, it ensures that only two reconnection actions can be performed at most during node plugging, and the maximum path to reach a certain element is at most twice the Shortest Path depth.
(4) set/multiset operation functions:
Set c generates an empty set/multiset
Set c (op) uses op as the sorting criterion to generate an empty set/multiset
Set c1 (c2) generates a copy of a set/multiset.
Set c (beg, end) generates a set/multiset with elements in the interval [beg, end)
Set c (beg, end, op) uses op as the sorting criterion and generates a set/multiset with elements in the range [beg, end)
C .~ Set () destroys all elements and releases memory
Set can be in the following forms: set <Elem>, set <Elem, op>, multiset <Elem>, and multiset <Elem, op>
C. size () returns the current number of elements
C. empty () determines whether c is empty
C. max_size () returns the maximum number of elements that can be accommodated.
C1 compare c2 compare = ,! =, <,>, <= And> =
C. count (elem) returns the number of elements whose "element value is elem ".
C. find (elem) returns the iterator of the first element whose value is elem. If no value is found, end () is returned ()
C. lower_bound (elem) returns the first pluggable location of elem, that is, the first element location of "element value> = elem ".
C. upper_bound (elem) returns the last pluggable location of elem, that is, the first element location of "element value> elem ".
C. when _range (elem) returns the first and last positions of elem, that is, the first element range of "element value = elem". The returned values are lower_bound () and upper_bound () the returned value is made into a pair.
C1 = c2 assign all c2 elements to c1
C1.swap (c2) SWAps c1 and c2 Elements
C. begin () returns a bidirectional access iterator (which treats the element as a constant) pointing to the first element.
C. end () returns a bidirectional access iterator (which treats the element as a constant) pointing to the next position of the last element.
C. rbegin () returns a reverse iterator pointing to the first element of the reverse iteration.
C. rend () returns a reverse iterator pointing to the next position of the last element of the reverse iteration.
C. insert (elem) inserts an elem copy and returns the location of the new element.
C. insert (it, elem) insert a copy of elem to return the location of the new element (it is a prompt to indicate the start point of the search for the insert operation)
C. insert (beg, end) All element copies in the insert interval [beg, end), no return value
C. erase (elem) removes all elements equal to elem and returns the number of removed elements.
C. erase (it) removes the elements at the it location and no response is returned.
C. erase (beg, end) removes all elements in the [beg, end) interval, and no response is returned.
C. clear () clear the container
Set provides the following interfaces:
Pair <iterator, bool> insert (const value_type & elem );
The second member in the pair structure indicates whether the plug-in is successful. The first member returns the position of the new element or the position of the existing element with the same value.
Iterator insert (iterator pos_hint, const value_type & elem );
Multiset provides the following interfaces:
Iterator insert (const value_type & elem );
Iterator insert (iterator pos_hint, const value_type & elem );