设为首页 加入收藏

TOP

c/c++ 标准库 map set 插入
2018-10-21 14:12:35 】 浏览:35
Tags:c/c 标准 map set 插入

标准库 map set 插入

一,插入操作

有map如下:

map<string, size_t> cnt;

插入方法:

插入操作种类 功能描述
cnt.insert({"abc", 12}); 直接用大括号
cnt.insert(make_pari("abc", 22)); 用make_pair函数,返回一个pair
cnt.insert(pair<string, size_t>("abc", 22)); 直接构造pair
cnt.insert(map<string, size_t>::value_type("abc",31)); 有map的value_type

插入注意:map,set没有重复的key,所以插入重复key的pair时,实际是没有插入进去的。

map<int, int> mp{{1,2},{2,3}};
mp.insert({1,3});//{1,3}的key和{1,2}重复了,所以mp还是原来的:{1,2},{2,3}

二,map和multimap的插入单一值的返回值

类型 功能描述
map 返回一个pair。first是迭代器,second是bool,插入成功second为true
multimap 返回一个迭代器

小例子索引

代码块 功能描述
test1 4种插入方式
test2 map insert的返回值
test3 multimap insert的返回值

小例子:

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

using namespace std;

int main(){
  //test1 4种插入方式                                                           
  /*                                                                            
  map<int, int> mp{{1,2},{2,3}};                                                
  //方式1                                                                       
  mp.insert({1,3});//insert不成功,因为key1已经存在                              
  //方式2                                                                       
  mp.insert(make_pair(3,4));                                                    
  //方式3                                                                       
  mp.insert(pair<int,int>(4,1));                                                
  //方式4                                                                       
  mp.insert(map<int,int>::value_type(5,1));                                     
  for(auto &s : mp){                                                            
    cout << s.first << "," << s.second << endl;                                 
  }                                                                             
  */

  //test2 map insert的返回值                                                    
  /*                                                                            
  map<string, size_t> cnt;                                                      
  string wd;                                                                    
  while(cin >> wd){                                                             
    //map和setinsert的返回值类型如下,用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;                                 
  }                                                                             
  */

  //test3 multimap insert的返回值                                               
  multimap<string, size_t> aus;
  aus.insert({"aaa", 1});
  //multi的返回值类型如下                                                       
  map<string, size_t>::iterator ret = aus.insert({"aaa", 2});
  cout << ret->first << ":" << ret->second << endl;
}

github完整代码

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇cf643E. Bear and Destroying Sub.. 下一篇DP问题

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目