Detailed explanation of the associated container map of C++ STL

Part.I Attention

insert image description here
In C++, map provides a key-value pair container, in which the data appears in pairs, and the first value in each pair is called a key (key), and each key can only be used in the map Appears once; the second is called the corresponding value of the keyword (Value).

Notes on Chap.I

mapPoints to note when using :

  • add citation#include <map>
  • mapIn addition to the header file, there are mapalso multimap; in addition, unordered_mapthere are similar containers in the header file unordered_map, unordered_multimap, and the differences between them are as follows:
name meaning
map In a map, the key value key is usually used to uniquely identify an element, and the value value stores the content associated with the key value key; the type of the key value key and value may be different, and inside the map, the key and value are passed through members The type value_type is bound together, and an alias is given for it pair; the key in the map is unique and cannot be modified, and the insertion will fail if a duplicate key is encountered; but []the value can be modified by using the operator; the underlying implementation of the map is red Black tree, the search efficiency is relatively high, yes O(logN);
multimap The difference with map is: the key in multimap can be repeated; there is no overloaded operator []function in multimap; for repeated elements, the first element of the in-order traversal is also returned when searching.
unordered_map Elements of the key-value pair type are stored <key, value>, where the values ​​of each key-value pair key are not allowed to be repeated, and the key-value pairs stored in the container are unordered.
unordered_multimap The only difference from unordered_map is that this container allows storing multiple key-value pairs with the same key.
  • The most basic constructor example:std::map<int, std::string> mapPerson;
  • mapThe elements in are stored in a certain order, which is sorted in ascending order by key by default;
map<T1, T2> m;	//默认按键的升序方式排列元素,相当于 map<T1, T2, less<T1>> m
map<T1, T2, less<T1>> m; //该容器是按键的升序方式排列元素。
map<T1, T2, greater<T1>> m;  //该容器是按键的降序方式排列元素。

Chap.II Operating Skills

Here are some examples of operations:

// 用insert函數插入pair
mapStudent.insert(make_pair(000, "student_zero"));
// 用insert函數插入pair
mapStudent.insert({
    
    000, "student_zero"});
// 用insert函數插入pair
mapStudent.insert(pair<int, string>(000, "student_zero"));
// 用"array"方式插入
mapStudent[123] = "student_first";
// 查找元素,没找到就返回 end
mapStudent.find("123")!=mapStudent.end()
// 根据键值删除元素,如果删除成功返回1,否则返回0
mapStudent.erase("123");
// 清空整个 map
mapStudent.erase(mapStudent.begin(), mapStudent.end());

Part.II Funciton

Only mapthe functions introduced here (the functions of the other three map are similar to it, some have fewer functions or more functions), as shown in the following figure:

insert image description here

function meaning
begin() Returns an iterator pointing to the head of the map (note that it is sorted)
clear() remove all elements
count(key) Returns keythe number of occurrences of the key, mapwhere the maximum value returned by this function is 1
empty() Returns true if the map is empty
emplace() Constructs a new key-value pair at the specified position in the current map container. Its effect is the same as inserting key-value pairs, but more efficient.
end() Returns an iterator pointing to the end of the map, note that it is not the last element, but the end of the container, similar to char[]the type in'\0'
equal_range() This method returns a pair object (contains two bidirectional iterators), where the return value of pair.firstthe and method are equivalent, and the return value of the method is equivalent. That is to say, this method will return a range that contains the key-value pair whose key is key (the map container key-value pair is unique, so the range contains at most one key-value pair).lower_bound()pair.secondupper_bound()
erase() Delete the specified location, specified key (key) value, or key-value pair in the specified area of ​​the map container.
find(key) Look up the key-value pair whose key is key in the map container. If it is successfully found, return a bidirectional iterator pointing to the key-value pair; otherwise, return the same end()iterator as the method.
get_allocator() Returns the configurator for the map, such asmap<string, int>::allocator_type
insert() insert element
key_comp() Returns a function for comparing element keys
lower_bound(key) Returns a bidirectional iterator pointing to the first key-value pair greater than or equal to key in the current map container
max_size() Returns the maximum number of elements that can hold
rbegin() Returns a reverse iterator pointing to the end of the map
rend() Returns a reverse iterator pointing to the head of the map
size() Returns the number of elements in the map
swap() swap two maps
upper_bound(key) Returns an iterator pointing to the first key-value pair greater than key in the current map container.
value_comp() Returns a function comparing element value

Part.III Code

Chap.I map

The test code is as follows:

#include <iostream>
#include <iomanip>
#include <map>

using namespace std;

int main()
{
    
    
    map<string,int> data {
    
    {
    
    "Tom",10},{
    
    "Jerry",20}};
    // map<string,int> data {make_pair("Tom",10),make_pair("Jerry",20)}    // the same as above
    auto tmp=data.begin();
    cout<< tmp->first << " " << tmp->second<<endl;
    data["Allice"]=30;
    data.insert({
    
    "Tony",10});
    cout<<data["Tony"] << endl;
    data.insert({
    
    "Tony",20}); // it can't work, data["Tony"]=20; is OK!
    cout<<data["Tony"] << endl;
    tmp=data.begin();
    cout<< tmp->first << " " << tmp->second<<endl;
    cout<<data.size() << setw(15)<<data.max_size()<<endl;
    for(auto itr=data.rbegin();itr!=data.rend();itr++)
        cout<< itr->first<<" " <<itr->second<< endl;
    getchar();
    return 0;
}

The output is as follows:

Jerry 20
10
10
Allice 30
4       97612893
Tony 10
Tom 10
Jerry 20
Allice 30

Chap.II unordered_map

The test code is as follows:

#include <iostream>
#include <iomanip>
#include <unordered_map>

using namespace std;

int main()
{
    
    
    unordered_map<string,int> data {
    
    {
    
    "Tom",10},{
    
    "Jerry",20}};
    // map<string,int> data {make_pair("Tom",10),make_pair("Jerry",20)}    // the same as above
    auto tmp=data.begin();
    cout<< tmp->first << " " << tmp->second<<endl;
    data["Allice"]=30;
    data.insert({
    
    "Tony",10});
    cout<<data["Tony"] << endl;
    data.insert({
    
    "Tony",20}); // it can't work, data["Tony"]=20; is OK!
    cout<<data["Tony"] << endl;
    tmp=data.begin();
    cout<< tmp->first << " " << tmp->second<<endl;
    cout<<data.size() << setw(15)<<data.max_size()<<endl;
    for(auto itr=data.begin();itr!=data.end();itr++)
        cout<< itr->first<<" " <<itr->second<< endl;
    getchar();
    return 0;
}

The output is as follows:

Jerry 20
10
10
Tony 10
4      119304647
Tony 10
Allice 30
Jerry 20
Tom 10

Chap.III multimap

The test code is as follows:

#include <iostream>
#include <iomanip>
#include <map>

using namespace std;

int main()
{
    
    
    multimap<string,int> data {
    
    {
    
    "Tom",10},{
    
    "Jerry",20}};
    // map<string,int> data {make_pair("Tom",10),make_pair("Jerry",20)}    // the same as above
    auto tmp=data.begin();
    cout<< tmp->first << " " << tmp->second<<endl;
    data.insert({
    
    "Allice",30});
    data.insert({
    
    "Tony",10});
    auto ret=data.equal_range("Tony");
    for(auto itr=ret.first;itr!=ret.second;itr++)
        cout<<itr->second<< " ";
    cout << endl;
    data.insert({
    
    "Tony",20}); // it work! data["Tony"]=20; is Error!
    ret=data.equal_range("Tony");
    for(auto itr=ret.first;itr!=ret.second;itr++)
        cout<<itr->second<< " ";
    cout << endl;
    tmp=data.begin();
    cout<< tmp->first << " " << tmp->second<<endl;
    cout<<data.size() << setw(15)<<data.max_size()<<endl;
    for(auto itr=data.begin();itr!=data.end();itr++)
        cout<< itr->first<<" " <<itr->second<< endl;
    getchar();
    return 0;
}

The output is as follows:

Jerry 20
10
10 20
Allice 30
5       97612893
Allice 30
Jerry 20
Tom 10
Tony 10
Tony 20

Guess you like

Origin blog.csdn.net/Gou_Hailong/article/details/128389898