1. Container type
Save elements in order BY keyword:
Map: used to save key value pairs, keywords can not be duplicated
Set: save only keywords, keywords cannot be duplicated
Multimap: used to save key-value pairs, the keyword can be repeated
Multiset: save only keywords, keywords can be repeated
Unordered collection:
Unordered_map: A map organized with a hash function
Unordered_set: Set that is organized with a hash function
Unordered_multimap: A map with a hash function toduplicate the keyword
Unordered_multiset: Set with hash function , keyword repeatable
2. Defining and Initializing containers
It can be initialized in 3 ways (and the same as in the previous order container initialization method):
1.c={a,b,c ...} Initialize as a list in parentheses
Map<string,string> word_count={{"A", "1"}, {"B", "2"}};
2. Initializing to another copy of the same type container
C C (C1) C initialized to C1 copy
3. Initialize the container according to the scope of the iterator
C C (b,e) copies the elements between the b,e iterators to C
3. Additional type aliases for associative containers
Key_type: container's keyword type
For set containers,key_type and value_type are the same
For a map container, the type that represents the keyword
Mapped_type: for maponly, the type of value associated with each keyword
Value_type: for set, same as Key_type
For map, represents pair<const key_type, mapped_type> key-value pair
Pair key pairs have two public members first and second, each representing keywords and values
4. adding elements
C.insert (v)
C.emplace (args)
For map,set: Only when the element is no longer in the container does it insert v, returning a pair, First the member represents an iterator that points to the keyword, Second member is a successful insert that indicates whether the BOOL value
For Multimap and multiset: Whenever you insert v, and return an iterator that points to the new element
C.insert (B,e)
C.insert (LI)
Represents an element inserted between an b,e iterator, inserting An element in an Li list, returning a void
For map,set: Only elements that are no longer in the container are inserted
For Multimap and multiset: inserting each element
C.insert (P,V)
C.empalce (P,args)
is not inserted before p (unlike the sequential container), but instead indicates that the search starts from the p iterator and is inserted into the specified position (because the elements in the ordered container are ordered)
5. Delete a container
C.erase (k): Delete the element with the keyword k and return the number of deleted elements
C.erase (P): removes the element that the p iterator points to, returns an iterator that points to the element following the deleted element, andp must point to The true element in C
C.erase (b,e): removes The element between iterators b,e, returns e
6. Accessing elements
C.find (k): returns an iterator pointing to the first keyword k , or, if no Kexists, returns the trailing iterator (c.end ())
C.count (k): returns the number of elements that are equal to K for the keyword, and for set and map, the number of elements is always 1 and 0
C.lower_bound (k): returns an iterator that points to the element >=k the first keyword
C.upper_bound (k): returns an iterator that points to the element >k the first keyword
C.equal_range (k): returns an iterator pairthat represents The range of elements of the keyword =k, if k does not exist ,pair Two members are c.end ()
Note: Use c.lower_bound (k) and C.upper_bound (k) to find k, if k exist, returning the first point to k iterator and the last point k The iterator after the element (not the last K Element), and if K does not exist, the two iterators are equal.
Associative Container Summary