C++stl-map/multimap container concept and usage

C++stl-map/multimap container concept and usage

Basic concepts of map:

Introduction:

All elements in the map are pairs

The first element in the pair is key (key value), which acts as an index, and the second element is value (real value)

All elements are automatically sorted according to the key value of the element

Nature:

map/multimap is an associative container, and the underlying structure is implemented with a binary tree

advantage:

You can quickly find the
difference between value value map and multimap based on the key value :
map does not allow duplicate key value elements in the
container multimap allows duplicate value key value elements in the container

map construction and assignment

Function description:

Construct and assign values ​​to the map container.
Function prototype:

structure:

 map<T1, T2>mp;             //map默认构造函数
map(const map &mp);  //拷贝构造函数

Assignment:

map& operator=(const map &mp);    //重载等号操作符

Code example:

#include<iostream>
#include<map>
using namespace std;
//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容器
       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>(4, 40));
       printMap(m);
       //拷贝构造
       map<int, int>m2(m);
       printMap(m2);
       //赋值
       map<int, int>m3;
       m3 = m2;
       printMap(m3);
}
int main()
{
    
    
       test01();
       system("pause");
       return 0;
}

Summary: All elements in the map appear in pairs, and pairs should be used when inserting data.

Guess you like

Origin blog.csdn.net/gyqailxj/article/details/114640284