C++STL common operations of map articles
Introduction:
#include<map>
Map is a standard associative container, and its elements are a pair (refer to the previous pair article here).
We denote its pair as: (key, value), which can be understood as key is a keyword, and value is its corresponding value.
The key value is unique in the map.
The realization of the map is realized by using a balanced binary tree of a red-black tree variant, in which the elements are sorted in a certain order, and the insertion process is also carried out according to the sorting rules.
1. Structure
map<int, string> mp1;
map<int, char> mp2;
//也可以是其他各种类型包括自定义结构体
2. Insert
(1) Insert through pair
map<int, string> mp1;
mp1.insert(pair<int, string>(1, "1111"));
(2) Insert through make_pair
mp1.insert(make_pair(2, "2222"));
(3) Insert through value_type
mp1.insert(map<int, string>::value_type(3, "3333"));
(4) Insert in an array-like manner
mp1[4] = "4444";
note!
The return value of insert is pair<iterator,bool>
In the fourth method, during the insertion operation, it will first find out whether there is an element with key 4 in mp1. If it is not found, then add a new node, assign its key to 4, and assign the string corresponding to key=4 to "4444"; if found, modify the string value corresponding to the original key=4 to "4444".
3. Commonly used functions
mp1.begin(); //返回mp1第一个元素的迭代器
mp1.end(); //返回mp1最后一个元素后面一个位置的迭代器
mp1.size(); //返回mp1中元素的个数
mp1.empty(); //判断mp1是否为空
mp1.find(1); //如果存在key值为1的元素,那么返回该元素的迭代器,否则返回mp1.end()
mp1.count(1); //返回key为1的元素的个数,由于map不允许key重复,所以返回值只能是0或1
The count function here can be greater than 1 for multimap.
The insert function has been mentioned before, and the delete function will be mentioned in the later part.
4. Delete
mp1.clear(); //清空mp1
mp1.erase(1); //删除key值为1的元素,并且返回下一个元素的迭代器
map<int, string>::iterator it = mp1.begin();
mp1.erase(it); //删除迭代器it所指向的元素,并返回下一个元素的迭代器
5. Comprehensive code
#include<iostream>
#include<map>
using namespace std;
int main() {
map<int, string> mp1;
mp1.insert(map<int, string>::value_type(3, "我是3"));
mp1[4] = "我是4";
mp1.insert(make_pair(2, "我是2"));
mp1.insert(pair<int, string>(1, "我是1"));
//插入元素
for (auto it = mp1.begin(); it != mp1.end(); ++it)
cout << it->first << ":" << it->second << endl;
cout << "-----------------------------" << endl;
//输出
mp1.erase(1); //删除key值为1的元素,并且返回下一个元素的迭代器
for (auto it = mp1.begin(); it != mp1.end(); ++it)
cout << it->first << ":" << it->second << endl;
cout << "-----------------------------" << endl;
//删除key值为1的元素后输出
map<int, string>::iterator it = mp1.begin();
mp1.erase(it); //删除迭代器it所指向的元素,并返回下一个元素的迭代器
for (auto it = mp1.begin(); it != mp1.end(); ++it)
cout << it->first << ":" << it->second << endl;
cout << "-----------------------------" << endl;
//再删除mp1第一个元素后输出
mp1.clear(); //清空mp1
for (auto it = mp1.begin(); it != mp1.end(); ++it)
cout << it->first << ":" << it->second << endl;
cout << "-----------------------------" << endl;
//清空后输出
if (mp1.empty())
cout << "mp1 is empty!" << endl;
cout << "mp1's size is " << mp1.size() << endl;
mp1.insert(make_pair(2, "我是2"));
cout << mp1.find(2)->second << endl;
cout << mp1.count(1);
//其他函数的使用
return 0;
}
Map basic application and operation
Please correct me if you find a problem!
Hope to help you! Please leave a message if you don’t understand!