日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

C++随手记——map.emplace and insert

發(fā)布時間:2023/12/13 综合教程 33 生活家
生活随笔 收集整理的這篇文章主要介紹了 C++随手记——map.emplace and insert 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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/

總結(jié)

以上是生活随笔為你收集整理的C++随手记——map.emplace and insert的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。