C++随手记——map.emplace and insert
1.map.emplace()
Inserts a new element in the map if its key is unique. This new element is constructed in place using args as the arguments for the construction of a value_type (which is an object of a pair type).
The insertion only takes place if no other element in the container has a key equivalent to the one being emplaced (keys in a map container are unique).
(无此key则加入map中,有则不加入。map中key是识别符)
If inserted, this effectively increases the container size by one.
Internally, map containers keep all their elements sorted by their key following the criterion specified by its comparison object. The element is always inserted in its respective position following this ordering.
The element is constructed in-place by calling allocator_traits::construct with args forwarded.
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 container size by the number of elements inserted.
Because element keys in a map are 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, see multimap.
An alternative way to insert elements in a map is by using member function map::operator[].
Internally, map containers keep all their elements sorted by their key following the criterion specified by its comparison 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:\n"; 39 for (it=mymap.begin(); it!=mymap.end(); ++it) 40 std::cout << it->first << " => " << it->second << '\n'; 41 42 std::cout << "anothermap contains:\n"; 43 for (it=anothermap.begin(); it!=anothermap.end(); ++it) 44 std::cout << it->first << " => " << it->second << '\n'; 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/