map container c ++ STL containers

1.map all elements are pair;

The first element is the element 2.pair key, the second element is the value;

3. All the elements are automatically sorted according to the key value;

4.map duplicate keys are not allowed, multimap duplicate keys are allowed;

Pros: can quickly find value in accordance with key;

A constructor

map<T1,T2> mp;
map(const map &mp);

Second, the assignment

map& operator=(const map &mp);

Three, map size and exchange

size();
empty();
swap(st);

Fourth, insert and delete

insert(ele);
clear();
erase(pos);
erase(beg,end);
erase(key);
#include<iostream>
using namespace std;
#include <map>

//map容器 插入和删除

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

void test01()
{
    Map< Int , int > m; 

    // insert
     // first 
    m.insert (pair < int , int > ( . 1 , 10 )); 

    // second 
    m.insert (the make_pair ( 2 , 20 is )); 

    / / third 
    m.insert (Map < int , int > :: the value_type ( . 3 , 30 )); 

    // fourth 
    m [ . 4 ] = 40 ; 

    // [] is not advisable to insert, use may be utilized to access key value
     //<< m COUT [. 4] << endl;
    printMap (m); 


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


    m.erase ( . 3 ); // accordance delete key 
    printMap (m); 

    // Clear
     // m.erase ( m.begin (), m.end ()); 
    m.clear (); 
    printMap (m); 
} 

int main () { 

    Test01 (); 

    System ( " PAUSE " ); 

    return  0 ; 
}

Fifth, find and statistics

void test01()
{
    //查找
    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));
    m.insert(pair<int, int>(3, 40));

    map<int,int>::iterator pos = m.find(3);

    if (pos != m.end())
    {
        cout << "查到了元素 key = " << (*pos).first << " value = " << pos->second << endl;
    }
    else
    {
        cout << "未找到元素" << endl;

    NUM = m.count (intthe multimap may be greater than the count statistics. 1//Map not allowed to insert a duplicate key element, count statistics in terms of the result is either 0 or. 1
    //Statistics
    //
    }
    3);
    cout << "num = " << num << endl;
}

Six, map sorted (ascending order)

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

class MyCompare
{
public:
    bool operator()(int v1,int v2) const
    {
        //降序
        return v1 > v2;
    }
};

//map容器 排序
void test01()
{
    map<int, int , MyCompare>m;

    m.insert(make_pair(1, 10));
    m.insert(make_pair(2, 20));
    m.insert(make_pair(5, 50));
    m.insert(make_pair(3, 30));
    m.insert(make_pair(4, 40));


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

int main() {

    test01();

    system("pause");

    return 0;
}

vs2019 require modified by const when overloaded operator ().

Guess you like

Origin www.cnblogs.com/xiximayou/p/12112290.html