Table of contents
1. Introduction to map
In C++, std::map
and multimap
are both associative containers , and are included in header files #include <map>
, and their underlying structures are implemented using binary trees .
map
All elements in arepair
,pair
the first element is the key value (key
), which acts as an index; the second element is the real value (value
), which is the mapped value- The elements in this type of container will be automatically sorted according to the key value ( ) of the element when inserted.
key
2. map constructor
Constructor prototype | explain | |
---|---|---|
1 | map<T1, T2> mp | default construction |
2 | map(const map &mp) | copy construction |
3. Map assignment operation
Function prototype: = | explain | |
---|---|---|
1 | set& operator=(const set &st) | Overloaded = operator |
4. Map data insertion and deletion
Function prototype: insert, erase, clear | explain | |
---|---|---|
1 | insert(elem) | Insert element into container |
2 | erase(key) | Delete the element whose key value is key in the container |
3 | erase(pos) | Delete the element pointed to by the pos iterator and return the iterator of the next element |
4 | erase(beg, end) | Delete all elements in the interval [beg, end) and return an iterator of the next element |
5 | clear() | Clear all elements |
Example 1:map<string, int>
#include <iostream>
#include <string>
#include <map> //必须包含该头文件
using namespace std;
void printmap(map<string,int>& s)
{
for (map<string,int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << "键值:" << it->first << "\t实值:" << it->second << endl;
}
cout << endl;
}
void test01()
{
map<string,int> s1;
//第1种插入方式
s1["娃哈哈"] = 2;
//第2种插入方式
s1.insert(pair<string,int>("可乐", 3));
//第3种插入方式
s1.insert(make_pair("脉动", 4));
//第4种插入方式
s1.insert(map<string, int>::value_type("红牛", 5));
cout << "插入后的元素:" << endl;
printmap(s1); //string类型key会根据首字母顺序
s1.erase("娃哈哈");
cout << "删除娃哈哈后的元素:" << endl;
printmap(s1);
s1.clear();
cout << "清空后的元素:" << endl;
printmap(s1);
}
int main()
{
test01();
system("pause");
return 0;
}
//result
插入后的元素:
键值:红牛 实值:5
键值:可乐 实值:3
键值:脉动 实值:4
键值:娃哈哈 实值:2
删除娃哈哈后的元素:
键值:红牛 实值:5
键值:可乐 实值:3
键值:脉动 实值:4
清空后的元素:
Notice:
map
The data inserted into the container will bekey
automatically sorted according to the type,string
and the typekey
will be sorted according to the first letter.map
Inserting elements with duplicate key values into the container will fail, and the number of elements will not change.
Example 2:map<int, double>
#include <iostream>
#include <string>
#include <map> //必须包含该头文件
using namespace std;
void printmap(map<int, double>& s)
{
for (map<int, double>::iterator it = s.begin(); it != s.end(); it++)
{
cout << "键值:" << it->first << "\t实值:" << it->second << endl;
}
cout << endl;
}
void test01()
{
map<int, double> s1;
//第1种插入方式
s1[1] = 1.001;
//第2种插入方式
s1.insert(pair<int, double>(4, 1.004));
//第3种插入方式
s1.insert(make_pair(2, 1.002));
//第4种插入方式
s1.insert(map<int, double>::value_type(3, 1.003));
cout << "插入后的元素:" << endl;
printmap(s1); //string类型key会根据首字母顺序
s1.erase(1);
cout << "删除key=1的元素:" << endl;
printmap(s1);
s1.clear();
cout << "清空后的元素:" << endl;
printmap(s1);
}
int main()
{
test01();
system("pause");
return 0;
}
//result
插入后的元素:
键值:1 实值:1.001
键值:2 实值:1.002
键值:3 实值:1.003
键值:4 实值:1.004
删除key=1的元素:
键值:2 实值:1.002
键值:3 实值:1.003
键值:4 实值:1.004
清空后的元素:
5. Map search and statistics
Function prototype: find, count | explain | |
---|---|---|
1 | find(key) | Find whether the key exists. If it exists, return the iterator of the key value element; if it does not exist, return map.end() |
2 | count(key) | Count the number of elements of key (for map, the result is 0 or 1) |
Notice:
find(key)
What is returned is an iterator,iterator->first
and the sum caniterator->second
be obtained bykey
value
- Because
map
the elements are not repeated,count()
0 or 1 is returned.
Example:
#include <iostream>
#include <string>
#include <map> //必须包含该头文件
using namespace std;
void test01()
{
map<string, int> s1;
//插入insert
s1["娃哈哈"] = 2;
s1.insert(pair<string, int>("可乐", 3));
s1.insert(make_pair("脉动", 4));
s1.insert(map<string, int>::value_type("红牛", 5));
//查找find
map<string, int>::iterator pos = s1.find("脉动");
if (pos != s1.end())
{
cout << "找到元素: key = " << pos->first << " value = " << pos->second << endl;
}
else
{
cout << "未到元素key!";
}
//统计count
int num1 = s1.count("可乐");
cout << "可乐 num = " << num1 << endl;
int num2 = s1.count("雪碧");
cout << "雪碧 num = " << num2 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
//result
找到元素: key = 脉动 value = 4
可乐 num = 1
雪碧 num = 0
6. Number of map elements and exchange
Function prototype: empty, size, swap | explain | |
---|---|---|
1 | empty() | Determine whether the container is empty |
2 | size() | Returns the number of elements in the container |
3 | swap(set<T> & st) | Swap the elements in two containers |
Note:swap()
The elements contained in the two containers exchanged must be of the same type.
7. map iterator
Function prototype: begin, end | explain | |
---|---|---|
1 | begin() | Returns an iterator pointing to the first element of the set container |
2 | end() | Returns an iterator pointing to the next position of the last element of the set container |
3 | rbegin() | Returns a reverse iterator pointing to the last element of the set container |
4 | rend() | Returns a reverse iterator pointing to the previous position of the first element of the set container |
5 | cbegin() | Returns a const iterator pointing to the first element of the set container |
6 | a few() | Returns a const iterator pointing to the next position of the last element of the set container |
7 | crbegin() | Returns a const reverse iterator pointing to the last element of the set container |
8 | crend() | Returns a const reverse iterator pointing to the previous position at the beginning of the set container |
Note:const iterator
It is an const
iterator pointing to a value. The iterator itself can be modified, but it cannot be used to modify the value it points to.
8. Map container sorting
- The default sorting rule of the map container is
key
from small to large values. - Using functors, you can change the sorting rules
- For custom data types, value sorting rules
map
must be specified , the same as [ set usage method ]key
set
Example:
#include <iostream>
#include <string>
#include <map> //必须包含该头文件
using namespace std;
//仿函数,重载operator()运算符
class Compare
{
public:
//Visual Studio的编译器此处需添加const,具有指定const-volatile类型的变量只能调用具有相同或更大const-volatile限定定义的成员函数
bool operator()(int v1, int v2)const
{
return v1 > v2;
}
};
void test01()
{
map<int,string,Compare> s1; //指定排序规则, 从大到小
s1.insert(pair<int, string>(1, "李四"));
s1.insert(pair<int, string>(3, "张三"));
s1.insert(pair<int, string>(4, "王五"));
s1.insert(pair<int, string>(2, "赵六"));
for (map<int, string>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << "键值:" << it->first << "\t实值:" << it->second << endl;
}
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
//result
键值:4 实值:王五
键值:3 实值:张三
键值:2 实值:赵六
键值:1 实值:李四
9. The difference between map and multimap
map
Do not allow duplicate elements in the containermultimap
Allow duplicate elements in the containermap
When inserting data, the insertion result will be returned, indicating whether the insertion is successful.multimap
Data is not detected so duplicate data can be inserted
Example :multimap
methodfind
#include <iostream>
#include <string>
#include <map> //必须包含该头文件
using namespace std;
void test01()
{
multimap<int, string> s1;
s1.insert(pair<int, string>(1, "李四"));
s1.insert(pair<int, string>(3, "张三"));
s1.insert(pair<int, string>(1, "丁一"));
s1.insert(pair<int, string>(4, "王五"));
s1.insert(pair<int, string>(2, "赵六"));
s1.insert(pair<int, string>(1, "吴二"));
int sum = s1.count(1);
cout << "key = 1的元素数量:" << sum << endl;
//因为map会自动排列,key相同的元素必定连续排列,所以从第一个find(1)位置遍历count(1)次
multimap<int, string>::iterator it = s1.find(1);
for (int i = 0; i != s1.count(1); i++,it++)
{
cout << "键值:" << it->first << "\t实值:" << it->second << endl;
}
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
//result
key = 1的元素数量:3
键值:1 实值:李四
键值:1 实值:丁一
键值:1 实值:吴二
Note: Because multimap
it will be automatically arranged, key
the same elements must be arranged continuously, so by find(1)
traversing from the first position count(1)
, you can obtain all key = 1
the key-value pair elements.