Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

map & multimap

Brief introduction:

  • All elements in the map are pairs

  • The first element in a pair is Key, which acts as an index, and the second element is value, which is a real value.

  • All elements are automatically sorted by their key value

Map/multimap is an associative container. The underlying structure is implemented using a binary tree

Advantages: You can quickly find a value based on the key value

The difference between map and multimap:

  • The map does not allow duplicate keys in the container

  • Multimap allows duplicate keys in containers

Map constructs and assignments

Function prototype:

  • The constructor
- map<T1,T2> mp; // Map's default constructor

- map(const map &mp); // Copy the constructor
Copy the code
  • The assignment
- map &operator= (const map &mp); // Override the equal operator
Copy the code

“Demo” :

#include <iostream>
#include <map>

using namespace std;
void printMap(map<int.int> &m)
{
    for (map<int.int>::iterator it = m.begin(a); it ! = m.end(a); it++) { cout <<"key = " << it->first << ", value = " << it->second << endl;
    }
    cout << endl;
}

void test01(a)
{
    // Default construction
    map<int.int> m;
    m.insert(pair<int.int> (1.10));
    m.insert(pair<int.int> (2.20));
    m.insert(pair<int.int> (3.30));
    printMap(m);

    // Copy the constructor
    map<int.int> m2(m);
    printMap(m2);

    // Override the equal operator
    map<int.int> m3;
    m3 = m2; / / assignment
    printMap(m3);
}

int main(a)
{
    test01(a);return 0;
}
Copy the code

Summary: All elements in a map come in pairs, and pair groups are used when inserting data

Map size and exchange

Function prototype:

- size(a);// Returns the number of elements in the container

- empty(a);// Check whether the container is empty

- swap(a);// Swap two collection containers
Copy the code

“Demo” :

#include <iostream>
#include <map>

using namespace std;

void printMap(map<int.int> &m)
{
    if (m.empty())
    {
        cout << " m is empty ." << endl;
    }
    else
    {
        cout << "m.size = " << m.size() << endl;
    }

    for (map<int.int>::iterator it = m.begin(a); it ! = m.end(a); it++) { cout <<"key = " << it->first << ", value = " << it->second << endl;
    }
    cout << endl;
}

void test01(a)
{
    map<int.int> m;
    m.insert(pair<int.int> (1.10));
    m.insert(pair<int.int> (2.20));
    m.insert(pair<int.int> (3.30));
    printMap(m);
}

void test02(a)
{
    map<int.int> m1;
    m1.insert(pair<int.int> (1.10));
    m1.insert(pair<int.int> (2.20));
    m1.insert(pair<int.int> (3.30));

    map<int.int> m2;
    m2.insert(pair<int.int> (1.100));
    m2.insert(pair<int.int> (2.200));
    m2.insert(pair<int.int> (3.300));

    cout << "===== before exchange =====" << endl;
    printMap(m1);
    printMap(m2);

    cout << "===== after exchange =====" << endl;
    m1.swap(m2);
    printMap(m1);
    printMap(m2);
}

int main(a)
{
    test01(a);test02(a);return 0;
}

Copy the code

Map Insert and delete

Function prototype:

- insert(elem); // Insert elements into the container

- clear(a);// Empty the container

- erase(pos); // Delete the element pointed to by the pos iterator and return the iterator for the next element

- erase(begin,end); // Remove the element in the interval [begin,end] and return an iterator for the next element

- erase(key); // Delete the element with the key value from the container
Copy the code

“Demo” :

#include <iostream>
#include <map>

using namespace std;

void printMap(map<int.int> &m)
{
    if (m.empty())
    {
        cout << " m is empty ." << endl;
    }
    else
    {
        cout << "m.size = " << m.size() << endl;
    }

    for (map<int.int>::iterator it = m.begin(a); it ! = m.end(a); it++) { cout <<"key = " << it->first << ", value = " << it->second << endl;
    }
    cout << endl;
}

void test01(a)
{
    // Insert elements
    map<int.int> m;
    // The first method of insertion
    m.insert(pair<int.int> (1.10));
    // Second insertion method
    m.insert(make_pair(2.20));
    // The third method of insertion
    m.insert(map<int.int> : :value_type(3.30));
    // The fourth method of insertion
    m[4] = 40;
    printMap(m);

    / / delete
    m.erase(m.begin());
    printMap(m);

    m.erase(3);
    printMap(m);

    / / to empty
    m.clear(a);printMap(m);
}

int main(a)
{
    test01(a);return 0;
}

Copy the code

Map lookup and statistics

Function prototype:

- find(key); // Find if the key exists, return the iterator of the element of the key; If not, return set.end();

- count(key); // Count the number of key elements
Copy the code

“Demo” :

#include <iostream>
#include <map>

using namespace std;

void test01(a)
{
    map<int.int> m;
    m.insert(pair<int.int> (1.10));
    m.insert(make_pair(2.20));
    m.insert(map<int.int> : :value_type(3.30));
    m[4] = 40;

    / / to find
    map<int.int>::iterator pos = m.find(3);
    if(pos ! = m.end())
    {
        cout << "Found element key =" << (*pos).first << " , value = " << (*pos).second << endl;
    }
    else
    {
        cout << "Element not found" << endl;
    }

    int num = m.count(2);
    cout << "num = " << num << endl;
}

int main(a)
{
    test01(a);return 0;
}
Copy the code

Sort the map container

Summary:

  • You can specify collation rules for the map container using the parsers

  • For custom data types, a map must specify collation rules, just like a set

“Demo” :

#include <iostream>
#include <map>
using namespace std;

class MyCompare
{
public:
    bool operator(a)(int v1, int v2)
    {
        returnv1 > v2; }};void printMap(map<int.int> &m)
{
    if (m.empty())
    {
        cout << " m is empty ." << endl;
    }
    else
    {
        cout << "m.size = " << m.size() << endl;
    }

    for (map<int.int>::iterator it = m.begin(a); it ! = m.end(a); it++) { cout <<"key = " << it->first << ", value = " << it->second << endl;
    }
    cout << endl;
}

void test01(a)
{
    map<int.int> m;
    cout << "Sort from smallest to largest by default." << endl;
    // Sort from smallest to largest by default
    m.insert(pair<int.int> (3.30));
    m.insert(make_pair(1.10));
    m.insert(map<int.int> : :value_type(2.20));
    printMap(m);

    cout << "Sorting from large to small using parsers" << endl;
    // Use the function to sort from large to small
    map<int.int, MyCompare> m2;
    m2.insert(pair<int.int> (3.30));
    m2.insert(make_pair(1.10));
    m2.insert(map<int.int> : :value_type(2.20));
    for (map<int.int, MyCompare>::iterator it = m2.begin(a); it ! = m2.end(a); it++) { cout <<"key = " << it->first << ", value = "<< it->second << endl; }}int main(a)
{
    test01(a);return 0;
}
Copy the code