Set and Multiset1. Structure
Set and Multiset sort the elements according to a particular sort principle. The difference is that multisets allows elements to be duplicated, and set does not allow duplicates.
As long as the type T is assignable, copyable, comparable (according to a sort criterion), it can be the element of set or multisets. If there is no special sorting principle, with the default less, the elements are operator < compared to complete the sorting.
The sorting criteria must adhere to the following principles:
- Must be "objected to", for operator <, if x < Y is true, Y<x is false.
- Must be "transitive", for operator <, if X<y is true, Y<z is true, then X<z is true.
- Must be "non-reflexive", for operator<, X<x is always false.
2. Ability
Like all standard associative containers, sets and multisets are usually done with a balanced binary tree.
The main advantage of automatic sorting is that the two-tree search element has good performance, and its search function algorithm has logarithmic complexity. However, automatic sorting also creates a limit that cannot be changed directly, because it disrupts the order in which the element is changed, and the old element must be removed before the new element is inserted. So sets and multisets have the following characteristics:
- Does not provide any manipulation elements that are directly used to access the element
- The element is accessed through an iterator.
3. Operation function3.1 Construction, copying, and destruction
Operation |
Effect |
Set C |
Produces an empty set/multiset with no elements |
Set C (OP) |
Using OP as the ordering criterion, an empty set/multiset is generated. |
Set C1 (C2) |
Produce a copy of a set/multiset, all elements are copied |
Set C (beg,end) |
Generates a set/multiset with all elements within the interval [beg,end] |
Set C (Beg,end, op) |
With OP as the ordering criterion, the elements within the interval [beg,end] produce a set/multiset |
C.~set () |
Destroy all elements and free up memory |
Set<elem> |
Produces a set, with (operator <) as the sorting criteria |
Set<elem,0p> |
Produces a set, with Op as the sorting criterion |
3.2 Non-volatile operation
Operation |
Effect |
C.size () |
Returns the current number of elements |
C.empty () |
Determines whether the size is zero, equal to 0 = = size (), higher efficiency |
C.max_size () |
Returns the maximum number of elements that can be accommodated |
C1 = = C2 |
Determine if C1 equals C2 |
C1! = C2 |
Determine if C1 is not equal to C2 (equal to!) ( C1==C2)) |
C1 < C2 |
Determine if C1 is less than C2 |
C1 > C2 |
Determine if C1 is greater than C2 |
C1 <= C2 |
Determine if C1 is less than or equal to C2 (equal to!) ( C2<C1)) |
C1 >= C2 |
Determine if C1 is greater than or equal to C2 (equal to!) ( C1<C2)) |
3.3 Special Search functions
Sets and multisets are optimized for element-fast search, providing special search functions, so the search function should be used preferentially to obtain logarithmic complexity rather than the linear complexity of the STL. For example, in 1000 element search, the logarithm complexity average 10 times, and the linear complexity average needs 500 times.
Operation |
Effect |
Count (Elem) |
Returns the number of element values to Elem |
Find (Elem) |
Returns the element value as the first element of Elem if no end () is returned |
Lower _bound (elem) |
Returns the first placement of the element value Elem, which is the first element position of the element value >= Elem |
Upper _bound (elem) |
Returns the last elem position of the element value, which is the first element position of the element value > Elem |
Equal_range (Elem) |
Returns the first and last position where the elem can be placed, i.e. the interval of the element value ==elem |
3.4 Assigning values
Operation |
Effect |
C1 = C2 |
Give all the elements of C2 to C1 |
C1.swap (C2) |
Swap elements of C1 and C2 |
Swap (C1,C2) |
Ibid., Global function |
3.5 iterator-related functions
Sets and Multisets iterators are bidirectional iterators, and all elements are treated as constants to the iterator operation, ensuring that you do not artificially alter the values of the elements, disrupting the established order, and therefore cannot invoke the variable algorithm, such as remove ().
Operation |
Effect |
C.begin () |
Returns a random access iterator that points to the first element |
C.end () |
Returns a random access iterator that points to the next position of the last element |
C.rbegin () |
Returns a reverse iterator that points to the first element of a reverse iteration |
C.rend () |
Returns a reverse iterator that points to the next position of the last element of the reverse iteration |
3.6 Inserting and deleting elements
The argument must be valid, the iterator must point to a valid position, the sequence start point cannot be located after the end point, and the element cannot be removed from the empty container.
Operation |
Effect |
C.insert (Elem) |
Inserts a elem copy, returning the new element position, regardless of whether the insert succeeds or not. |
C.insert (POS, Elem) |
Insert a copy of the Elem element, return the new element position, POS as the starting point of the collection, increase the insertion speed. |
C.insert (Beg,end) |
Place all elements of the interval [beg,end] into C, with no return value. |
C.erase (Elem) |
Removes all elements equal to Elem and returns the number of elements removed. |
C.erase (POS) |
Removes the position element referred to by the iterator Pos, with no return value. |
C.erase (Beg,end) |
Removes all elements of the interval [beg,end], with no return value. |
C.clear () |
Remove all elements to empty the container |
C + + STL--Set and Multiset