[C ++ STL] STL in unordered_map

Keke is not a good learning C ++ is now in big trouble in school. What will not be
good then to add some simple operations on it unordered_map

definition

It's called: disorderly mapping.
The C ++ STL unordered_map implemented using a hash table lookup to realize the elements in O (1) time, but in the overhead space correspondingly increased.
Some compare its characteristics and map the information, my C ++ Cubs also be thrown off balance, not being expanded, features red-black tree I have not seen it ...
map data structure corresponding to the STL is red-black tree when data in the red-black tree orderly, time looking at the complexity of the red-black tree is O (logN), with respect to unordered_map query speed has dropped, but the extra space overhead is reduced.

Common Functions

statement

#include <unordered_map>
using namespace std;
// <.., ..> 中指明两个变量类型,key-value
unordered_map<string, int> map; 

Container size

cout << map.empty() << endl;
cout << map.size() << endl;

Insert key-value pairs

map['A'] = 1; 
// or
map.insert(make_pair('A', 1));

Determining whether there is a key value

Suppose we want to test the member function 'B' if the map we have just stated, you can use the unordered_map: find () function and end () function.
Note that find () and end () return data types are iterator.
In unordered_map, if the find () did I find the key, and returns end () as the iterator value.

// 'B' is not a key
if(map.find('B') == map.end()) {
    // your code
}
// or
if(map.count('B') == 0) {
    // your code
}

Remove elements

There are three types of elements removed, in accordance with the scope and delete key values.

//删除元素
second.erase ( second.begin() );      // erasing by iterator
second.erase ('a');             // erasing by key
second.erase ( second.find('c'), second.end() ); // erasing by range

other

map.clear(); // 清空

Guess you like

Origin www.cnblogs.com/recoverableTi/p/12641366.html