The STL map

 

An associated container:

By keyword order to save the elements
map associative array; save key - value pairs
set key value that is, save only the container keyword
map multimap keyword recurring
set multiset keyword recurring

unordered associative containers
unordered_map using a hash function organized map, chaotic
unordered_set set a hash function organized disorder
map unordered_multimap hash organizations; keyword can be repeated
unordered_multiset set hash organization, keywords can be repeated

Description:
. 1, container map: the map is performed by the underlying red-black tree, each node represents the red-black tree of a map element. The data structure having the function of automatic sorting, so the interior of the map elements are ordered, the order of elements in the container is determined by comparing the key value. Default less <K> object comparison.

2, the container multimap: Similar to the container map, multimap only difference is that the container can be saved same key elements.

3, unordered_map container: the container bottom by Hach (aka hash) function to achieve tissue. Order of the elements is not determined by the key, but is determined by the key-hash value, the hash value is generated by a hash function integer. Using the hash function, the hash value of the keywords are placed in a bucket (bucket) which has the same hash value into the same bucket. Unordered_map internal memory element is disordered, but also does not allow duplicate key elements corresponds to the java HashMap.

4, unordered_multimap containers: position of the object can also be determined by the value of the hash key generated, but it allows duplicate elements.

map and multimap container templates are defined in the map header file, unordered_map and templates unordered_multimap container are defined in the header file unordered_map.

multi prefix indicates keys need not be unique, but if you do not have this prefix, the key must be unique.
unordered prefix indicates the position of the container element is a hash value generated by its value is determined, not by comparing the decision key, i.e., the container element is disordered. Without this prefix, the container is determined by comparing the key elements of that order.

Two, map container

2.1 map初始化:
map<string, string> authors = { {"Joyce", "James"}, {"Austen", "Jane"}, {"Dickens", "Charles"} };

Insert data 2.2
. 1) INSERT
Map <String, int> mapStudent; // Create Map
mapStudent.insert (pair <String, int> ( "student_one", 22 is));

或者使用make_pair
map<string, int> mapStudent;
mapStudent.insert(make_pair("student_one", 22));

2)map<string, int> mapStudent;//创建map
mapStudent.insert(map<string, int>::value_type("student_one", 22));

3) array mode
map <string, int> mapStudent; // Create Map
mapStudent [ "student_one"] = 22 is;

3) data access and traverse

map and find common access method elements are:
======================================== ==================================================
operator [] access element may also be used to modify the value of the value of an element; not subscript (key) to check the presence or absence (i.e., if the keyword does not exist, the program does not run error), access to an element while,
if the key element is not present the element directly created, is returned to the initial value (such as initial int type is 0, 0 is returned, String initially NULL, NULL is returned)
AT access elements, can also be used modify the value to the value of an element; will be subscript (key) checks for the existence, if the keyword does not exist, an exception will be thrown out_of_range.
================================================== ========================================
use an iterator to access elements
***** ************************************************** ***********************************
Map <K, T> :: Iterator IT;
(IT *) .first; // The Key value (of type Key)
(IT *) .second; // The mapped value (of type T)
(IT *); // The "Element value" (type of pair <const Key, T>)
Values of key and value elements are first and second iterator properties. It can also be directly accessed using the iterator pointer.
IT-> First; // Same AS (IT *) .first (The Key value)
IT-> SECOND; // Same AS (IT *) .second (The mapped value)

Iterator member functions:
the begin returns a pointer to the starting position of the container iterator (Iterator)
End returns a pointer to the last position of the container iterator
cbegin normally returned iterator (a const_iterator) pointing to the start position of the container
cend often return point at the end position of the container iterator (iterator often used when no write access to the elements)
rbegin reverse iterator returns (a reverse_iterator) pointing to the start position of the container
rend reverse iterator to return the container end position
####### ################################################## ################################
find whether there is a key element:
COUNT if present, return 1; there is no return 0
Find if present iterator returns pointers pointing to an element, there is no return end ()

4) map traversal

① before using iterators to traverse the map

map<string, int>::iterator iter;
for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
      cout << iter->first << " " << iter->second << endl;

for (map<string, int>::iterator iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

     cout << (*iter).first << " " << (*iter).second << endl;

Both forms of access you can change the value of value of elements that can be written to access:

map<string, int> mapStudent;//创建map
mapStudent["student_one"] = 22;
mapStudent["student_two"] = 25;
mapStudent["student_three"] = 21;

map<string, int>::iterator iter;
for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
(*iter).second = 100; //将mapStudent中的元素value值全部改为100

for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

     cout << iter->first << " " << iter->second << endl; //遍历

If you use const_iterator, the two methods can not write access.

② using a reverse iterator to traverse map

Map <String, int> :: a reverse_iterator ITER;
for (ITER mapStudent.rbegin = (); ITER mapStudent.rend = ();! ITER ++)
    COUT << iter-> First << "" << iter-> SECOND < <endl; // reverse traversal

③ auto keyword

for (auto it = mapStudent.begin(); it != mapStudent.end(); it++)

      cout << it->first << " " << it->second << endl; //遍历

④ use arrays way to iterate

5) data deletion

Use the erase function

int res = mapStudent.erase ( "student_one" ); // delete successful return 1, else return 0; only use the return value only when deleting keywords
cout << "RES =" RES << << endl;

// use remove iterator
Map <String, int> :: iterator ITER;
ITER = mapStudent.find ( "student_two");
mapStudent.erase (ITER);

// delete a range of iterators element
auto it = mapStudent.begin () ;
mapStudent.erase (IT, mapStudent.find ( "student_two"));

Three, multimap container

Multimap containers are stored ordered key / value pairs, but may be saved duplicate elements. multimap sequences with the same key elements will appear. Most members use multimap and map the same function. Because of duplicate keys, multimap there are some ways to use functions and map there are some differences.

1, access to the elements

multimap subscript operator is not supported, because the key can not determine a unique element. And similar map, multimap can not be used at () function.

 

Guess you like

Origin www.cnblogs.com/sunshine1218/p/12081513.html