pair
1
2
3
4
5
6
7
8
9
10
11
#include <utility> // std::pair, std::make_pair
pair <string,double> product1; // default constructor
pair <string,double> product2 ("tomatoes", 2.30); // value init
pair <string,double> product3 (product2); // copy constructor
product1 = make_pair(string("lightbulbs"), 0.99); // using make_pair (move)
g2 = make_pair(1, 'a');
product2.first = "shoes"; //赋值
product2.second = 39.90;
操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ms.insert(make_pair('b', 25));
mymap.insert ( pair<char,int>('a',100) ); //map_name.insert({key, element})
mymap.insert (it, pair<char,int>('b',300));
anothermap.insert(mymap.begin(), mymap.find('c'));
map<char,int> mymap;
mymap.emplace('x',100); //map_name.emplace(key, element)
删除:
mymap.erase (it);
mymap.erase ('c'); // erasing by key
mymap.erase ( it, mymap.end() ); // erasing by range
count(key) //Count elements with a specific key
所有容器类 (vector, stack, queue, set, map, etc) 都支持 insert and emplace 操作.
emplace不copy of object.
参考
https://www.cplusplus.com/reference/utility/pair/pair/
https://www.geeksforgeeks.org/pair-in-cpp-stl/
https://www.geeksforgeeks.org/map-associative-containers-the-c-standard-template-library-stl/