c/c + + standard library map set insertion

Keywords: C++ github

Standard library map set insertion

1, Insert operation

The map is as follows:

map<string, size_t> cnt;

Insert method:

Insert operation type Function description
cnt.insert({"abc", 12}); Use braces directly
cnt.insert(make_pari("abc", 22)); Return a pair with the make u pair function
cnt.insert(pair<string, size_t>("abc", 22)); Direct construction of pair
cnt.insert(map<string, size_t>::value_type("abc",31)); value_type with map

Note: map and set do not have duplicate keys, so when inserting the pair of duplicate keys, they are not actually inserted.

map<int, int> mp{{1,2},{2,3}};
mp.insert({1,3});//The key of {1,3} and {1,2} are repeated, so mp is still the original: {1,2}, {2,3}

2, Insert single value return value of map and multimap

type Function description
map Return a pair. first is the iterator, second is the bool, and second is true after inserting successfully
multimap Returns an iterator

Small example index

Code block Function description
test1 4 Insertion method
test2 map insert Return value
test3 multimap insert Return value

Small example:

#include <iostream>
#include <set>
#include <map>
#include <vector>

using namespace std;

int main(){
  //test1 4 insertion methods                                                           
  /*                                                                            
  map<int, int> mp{{1,2},{2,3}};                                                
  //Mode 1                                                                       
  mp.insert({1,3});//insert Unsuccessful because key1 already exists                              
  //Mode 2                                                                       
  mp.insert(make_pair(3,4));                                                    
  //Mode 3                                                                       
  mp.insert(pair<int,int>(4,1));                                                
  //Mode 4                                                                       
  mp.insert(map<int,int>::value_type(5,1));                                     
  for(auto &s : mp){                                                            
    cout << s.first << "," << s.second << endl;                                 
  }                                                                             
  */

  //Return value of test2 map insert                                                    
  /*                                                                            
  map<string, size_t> cnt;                                                      
  string wd;                                                                    
  while(cin >> wd){                                                             
    //map The return value types of setinsert and setinsert are as follows. You can also use auto                               
    pair<map<string, size_t>::iterator, bool> ret =                             
      cnt.insert({wd, 1});                                                      
    //auto ret = cnt.insert({wd, 1});                                           
    if(!ret.second){                                                            
      ++ret.first->second;                                                      
    }                                                                           
  }                                                                             
  for(auto &s : cnt){                                                           
    cout << s.first << ":" << s.second << endl;                                 
  }                                                                             
  */

  //Return value of test3 multimap insert                                               
  multimap<string, size_t> aus;
  aus.insert({"aaa", 1});
  //The return value types of multi are as follows                                                       
  map<string, size_t>::iterator ret = aus.insert({"aaa", 2});
  cout << ret->first << ":" << ret->second << endl;
}

github full code

QQ group of mutual learning in c/c + +: 877684253

My wechat: xiaoshitou5854

Posted by serbestgezer on Fri, 27 Dec 2019 09:53:01 -0800