C/C++ - summary of the basic operations of map

The standard library map type is a key-value data type. The following is a summary of the following aspects:

  • Definition and initialization of map objects
  • Basic operations of map objects, including adding elements, traversing, etc.

1. Pair type

1.1. Definition and initialization of pair type

The pair type is defined in the utility file. The pair type contains two data values. There are usually the following definitions and initialization methods:

  • pair<T1, T2> p;
  • pair<T1, T2> p(v1, v2);
  • make_pair(v1, v2)

The first method above is to define an empty pair object p, and the second method is to define a pair object p with initial values ​​v1 and v2. The third method is to create a new pair object with the v1 and v2 values.

1.2. Some operations on pair objects

In addition, the pair object has some methods, such as taking out the value of each member in the pair object:

  • p.first
  • p.second

E.g:

#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
using namespace std;

int main(){
        pair<int, string> p1(0, "Hello");
        printf("%d, %s\n", p1.first, p1.second.c_str());
        pair<int, string> p2 = make_pair(1, "World");
        printf("%d, %s\n", p2.first, p2.second.c_str());
        return 0;
}

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

2. Definition and initialization of map object

A map is a combination of key-value pairs, with the following methods defined:

  • map<k, v> m;
  • map<k, v> m(m2);
  • map<k, v> m(b, e);

The first method above defines an empty map object named m; the second method creates a copy m of m2; the third method creates a map object m and stores all the elements in the range of iterators b and e a copy of.

The value_type of the map is the key of the stored element and the pair type of the value, and the key is const.

3. Some basic operations of map objects

3.1. Insertion of elements in map

There are two methods of inserting elements in a map:

  • use subscript
  • Use the insert function

Using a subscript to access a non-existing element in the map will cause a new element to be added to the map container.

The main insertion methods of the insert function are as follows:

  • m.insert(e)
  • m.insert(beg, end)
  • m.insert(iter, e)

The above e is a value of type value_type. Beg and end mark the start and end of the iterator.

The two insertion methods are shown in the following example:

#include <stdio.h>
#include <map>
using namespace std;

int main(){
        map<int, int> mp;
        for (int i = 0; i < 10; i ++){
                mp[i] = i;
        }
        for (int i = 10; i < 20; i++){
                mp.insert(make_pair(i, i));
        }
        map<int, int>::iterator it;
        for (it = mp.begin(); it != mp.end(); it++){
                printf("%d-->%d\n", it->first, it->second);
        }
        return 0;
}

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

3.2. Find and read elements in map

Note: When the element in the map is read using the subscript method above, if the element does not exist in the map, it will be inserted in the map.

Therefore, if you just want to find out whether the element exists, you can use the function count(k), which returns the number of occurrences of k; if you want to get the value corresponding to the key, you can use the function find(k), which returns an iterator pointing to the element.

The use of the above two functions is as follows:

#include <stdio.h>
#include <map>
using namespace std;

int main(){
        map<int, int> mp;
        for (int i = 0; i < 20; i++){
                mp.insert(make_pair(i, i));
        }

        if (mp.count(0)){
                printf("yes!\n");
        }else{
                printf("no!\n");
        }

        map<int, int>::iterator it_find;
        it_find = mp.find(0);
        if (it_find != mp.end()){
                it_find->second = 20;
        }else{
                printf("no!\n");
        }

        map<int, int>::iterator it;
        for (it = mp.begin(); it != mp.end(); it++){
                printf("%d->%d\n", it->first, it->second);
        }
        return 0;
}
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

3.3, delete elements from the map

The function that removes elements from the map is erase(), the function has the following three forms:

  • m.erase(k)
  • m.erase(p)
  • m.erase(b, e)

The first method deletes the element whose key is k in m, and returns the number of deleted elements; the second method deletes the element pointed to by the iterator p, and returns void; the third method deletes is the element in the range of iterator b and iterator e, and returns void.

As follows:

#include <stdio.h>
#include <map>
using namespace std;

int main(){
        map<int, int> mp;
        for (int i = 0; i < 20; i++){
                mp.insert(make_pair(i, i));
        }

        mp.erase(0);

        mp.erase(mp.begin());

        map<int, int>::iterator it;
        for (it = mp.begin(); it != mp.end(); it++){
                printf("%d->%d\n", it->first, it->second);
        }


        return 0;
}

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
                    <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/production/markdown_views-ea0013b516.css">
                        </div>

The standard library map type is a key-value data type. The following is a summary of the following aspects:

  • Definition and initialization of map objects
  • Basic operations of map objects, including adding elements, traversing, etc.

1. Pair type

1.1. Definition and initialization of pair type

The pair type is defined in the utility file. The pair type contains two data values. There are usually the following definitions and initialization methods:

  • pair<T1, T2> p;
  • pair<T1, T2> p(v1, v2);
  • make_pair(v1, v2)

The first method above is to define an empty pair object p, and the second method is to define a pair object p with initial values ​​v1 and v2. The third method is to create a new pair object with the v1 and v2 values.

1.2. Some operations on pair objects

In addition, the pair object has some methods, such as taking out the value of each member in the pair object:

  • p.first
  • p.second

E.g:

#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
using namespace std;

int main(){
        pair<int, string> p1(0, "Hello");
        printf("%d, %s\n", p1.first, p1.second.c_str());
        pair<int, string> p2 = make_pair(1, "World");
        printf("%d, %s\n", p2.first, p2.second.c_str());
        return 0;
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

2. Definition and initialization of map object

A map is a combination of key-value pairs, with the following methods defined:

  • map<k, v> m;
  • map<k, v> m(m2);
  • map<k, v> m(b, e);

The first method above defines an empty map object named m; the second method creates a copy m of m2; the third method creates a map object m and stores all the elements in the range of iterators b and e a copy of.

The value_type of the map is the key of the stored element and the pair type of the value, and the key is const.

3. Some basic operations of map objects

3.1. Insertion of elements in map

There are two methods of inserting elements in a map:

  • use subscript
  • Use the insert function

Using a subscript to access a non-existing element in the map will cause a new element to be added to the map container.

The main insertion methods of the insert function are as follows:

  • m.insert(e)
  • m.insert(beg, end)
  • m.insert(iter, e)

The above e is a value of type value_type. Beg and end mark the start and end of the iterator.

The two insertion methods are shown in the following example:

#include <stdio.h>
#include <map>
using namespace std;

int main(){
        map<int, int> mp;
        for (int i = 0; i < 10; i ++){
                mp[i] = i;
        }
        for (int i = 10; i < 20; i++){
                mp.insert(make_pair(i, i));
        }
        map<int, int>::iterator it;
        for (it = mp.begin(); it != mp.end(); it++){
                printf("%d-->%d\n", it->first, it->second);
        }
        return 0;
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

3.2. Find and read elements in map

Note: When the element in the map is read using the subscript method above, if the element does not exist in the map, it will be inserted in the map.

Therefore, if you just want to find out whether the element exists, you can use the function count(k), which returns the number of occurrences of k; if you want to get the value corresponding to the key, you can use the function find(k), which returns an iterator pointing to the element.

The use of the above two functions is as follows:

#include <stdio.h>
#include <map>
using namespace std;

int main(){
        map<int, int> mp;
        for (int i = 0; i < 20; i++){
                mp.insert(make_pair(i, i));
        }

        if (mp.count(0)){
                printf("yes!\n");
        }else{
                printf("no!\n");
        }

        map<int, int>::iterator it_find;
        it_find = mp.find(0);
        if (it_find != mp.end()){
                it_find->second = 20;
        }else{
                printf("no!\n");
        }

        map<int, int>::iterator it;
        for (it = mp.begin(); it != mp.end(); it++){
                printf("%d->%d\n", it->first, it->second);
        }
        return 0;
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

3.3, delete elements from the map

The function that removes elements from the map is erase(), the function has the following three forms:

  • m.erase(k)
  • m.erase(p)
  • m.erase(b, e)

The first method deletes the element whose key is k in m, and returns the number of deleted elements; the second method deletes the element pointed to by the iterator p, and returns void; the third method deletes is the element in the range of iterator b and iterator e, and returns void.

As follows:

#include <stdio.h>
#include <map>
using namespace std;

int main(){
        map<int, int> mp;
        for (int i = 0; i < 20; i++){
                mp.insert(make_pair(i, i));
        }

        mp.erase(0);

        mp.erase(mp.begin());

        map<int, int>::iterator it;
        for (it = mp.begin(); it != mp.end(); it++){
                printf("%d->%d\n", it->first, it->second);
        }


        return 0;
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
                    <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/production/markdown_views-ea0013b516.css">
                        </div>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326384906&siteId=291194637