Pair <T1, T2> P1;
Pair <T1, T2> P1 (V1, V2); // V1 is T1, V2 is T2
Make_pair (V1, V2); // return a pair object
P. first;
P. Second;
The associated container cannot be defined by container size or resize.
Map <K, V> M (m2 );
Map <K, V> m;
Map <K, V> M (B, E); // B, e is the map iterator.
Key must support <.
Map <K, V >:: value_type is pair type const key_type mapped_type
Map <K, V >:: mapped_type is the value type associated with the key.
Map <K, V >:: key_type is the key type.
MAP is easy to add elements. Similar to subscript.
For example, Map <string, int> word;
Word ["A"] = 1; // insert (make_pair ("A", 1 );
Word ["A"] returns an association value.
Therefore, map can be used to easily record the number of times a word appears.
Map <string, int> word_count;
String word;
While (CIN> word)
{
Word_count [word] ++;
}
Insert of Map
M. insert (E); // For pair, an iterator of E and bool are returned to determine whether the insert is successful.
M. insert (beg, end); // return void
M. insert (ITER, e); // ITER is the aid, returns the iterator, pointing to elements with specific keys in m
M. Count (k); // returns the number of K occurrences in M.
M. Find (k); // whether the element indexed by K exists. The element iterator is returned.
M. Erase (k); // delete an element whose key is K. The size_type type is returned, indicating the number of deleted elements.
M. Erase (p); // Delete the element referred to by iterator P and return void
M. Erase (B, E); // Delete the elements of B and E and return void.
Map can be used as word conversion, phone book, and dictionary.
Set does not support subscript operators,
Insert (key); // return pair <iterator, bool>;
Set can help map to record the word exclusion set.
Multimap does not support subscript operations.
Insert always adds elements. Erase deletes all matching elements and returns the number of deleted elements.
In multimap, the elements associated with a key are inevitably stored adjacent to each other. Therefore, after finding the first key, all elements can be found as long as the iterator ++.