C++随手记——map.emplace and insert
1.map.emplace()
Inserts a new element in themapif its key is unique. This new element is constructed in place usingargsas the arguments for the construction of avalue_type(which is an object of apairtype).
The insertion only takes place if no other element in the container has a key equivalent to the one being emplaced (keys in amapcontainer are unique).
(無此key則加入map中,有則不加入。map中key是識別符)
If inserted, this effectively increases the containersizeby one.
Internally,mapcontainers keep all their elements sorted by their key following the criterion specified by itscomparison object. The element is always inserted in its respective position following this ordering.
The element is constructed in-place by callingallocator_traits::constructwithargsforwarded.
A similar member function exists,insert, which either copies or moves existing objects into the container.
template <class... Args> pair<iterator,bool> emplace (Args&&... args);
2.map.insert()
共有三種方法,返回值各不相同,注意!
Extends the container by inserting new elements, effectively increasing the containersizeby the number of elements inserted.
Because element keys in amapare unique, the insertion operation checks whether each inserted element has a key equivalent to the one of an element already in the container, and if so, the element is not inserted, returning an iterator to this existing element (if the function returns a value).
For a similar container allowing for duplicate elements, seemultimap.
An alternative way to insert elements in amapis by using member functionmap::operator[].
Internally,mapcontainers keep all their elements sorted by their key following the criterion specified by itscomparison object. The elements are always inserted in its respective position following this ordering.
The parameters determine how many elements are inserted and to which values they are initialized:
/*single element (1)*/ pair<iterator,bool> insert (const value_type& val); /*with hint (2) */ iterator insert (iterator position, const value_type& val); /*range (3) */ template <class InputIterator> void insert (InputIterator first, InputIterator last);
3.實例代碼
1 #include <iostream>
2 #include <map>
3 using std::cout;
4 using std::endl;
5
6 /*
7 emplace:返回一對pair<iter,true/false>,iter指向插入的位置,即key所在的位置。
8 如果元素的key存在的話,并不會添加進map中。false
9 如果不存在,true
10 insert:也是插入值,判斷條件是一樣的。但是insert的方法比較多,注意返回值。
11 */
12 void map_insert()
13 {
14 std::map<char,int> mymap;
15
16 // first insert function version (single parameter):
17 mymap.insert ( std::pair<char,int>('a',100) );
18 mymap.insert ( std::pair<char,int>('z',200) );
19
20 std::pair<std::map<char,int>::iterator,bool> ret;
21 ret = mymap.insert ( std::pair<char,int>('z',500) );
22
23 cout << "ret.second: " << ret.second << endl;
24 cout << "ret.first->first: " << ret.first->first << endl;
25 cout << "ret.first->second: " << ret.first->second << endl;
26
27 /*注意:其他兩種insert的方法返回值不一樣*/
28 // second insert function version (with hint position):
29 std::map<char,int>::iterator it = mymap.begin();
30 mymap.insert (it, std::pair<char,int>('b',300)); // max efficiency inserting
31 mymap.insert (it, std::pair<char,int>('c',400)); // no max efficiency inserting
32
33 // third insert function version (range insertion):
34 std::map<char,int> anothermap;
35 anothermap.insert(mymap.begin(),mymap.find('c'));
36
37 // showing contents:
38 std::cout << "mymap contains:
";
39 for (it=mymap.begin(); it!=mymap.end(); ++it)
40 std::cout << it->first << " => " << it->second << '
';
41
42 std::cout << "anothermap contains:
";
43 for (it=anothermap.begin(); it!=anothermap.end(); ++it)
44 std::cout << it->first << " => " << it->second << '
';
45 }
46
47 void map_emplace()
48 {
49 std::map<char,int> mymap;
50
51 auto ret = mymap.emplace('x',100);
52 std::cout << " [ret.second :" << ret.second << ']' << std::endl;
53
54
55 cout << "ret.first->first: " << ret.first->first << endl;
56 cout << "ret.first->second: " << ret.first->second << endl;
57 mymap.emplace('y',100);
58 mymap.emplace('x',300);
59 mymap.emplace('a',100);
60
61 cout << endl << endl << "after mymap.emplace('x',200) " << endl << endl << endl;
62
63 auto ret2 = mymap.emplace('x',200);
64 std::cout << " [ret2.second :" << ret2.second << ']' << std::endl;
65
66 cout << "ret2.first->first: " << ret2.first->first << endl;
67 cout << "ret2.first->second: " << ret2.first->second << endl;
68 }
69
70 int main ()
71 {
72 map_insert();
73
74 system("pause");
75 return 0;
76 }
參考資料:
http://www.cplusplus.com/reference/map/map/emplace/
http://www.cplusplus.com/reference/map/map/insert/
總結
以上是生活随笔為你收集整理的C++随手记——map.emplace and insert的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 无法载入 mysql 扩展
- 下一篇: 【备忘】XP欢迎页中隐藏用户名