stl中map的四种插入方法总结
方法一:pair
例:
map<int, string> mp;
mp.insert(pair<int,string>(1,"aaaaa"));

方法二:make_pair
例:
map<int, string> mp;
mp.insert(make_pair<int,string>(2,"bbbbb"));

方法三:value_type
例:
map<int, string> mp;
mp.insert(map<int, string>::value_type(3,"ccccc"));

方法四:[]
例:
map<int, string> mp;
mp[4] = "ddddd";

四种方法异同:
前三种方法当出现重复键时,编译器会报错,而第四种方法,当键重复时,会覆盖掉之前的键值对。

综合测试:

#include<iostream>
#include<map>
using namespace std; int main()
{
map<int, string> mp;
//map的插入方法有4种
//insert返回值为pair 原型:typedef pair<iterator, bool> _Pairib
//方法1.pair 在插入重复键的情况下前三种方法类似,这里只测试第一种
pair<map<int,string>::iterator, bool> pair1 = mp.insert(pair<int,string>(,"aaaaa11111"));
if (pair1.second == true)
{
cout<< "插入成功" <<endl;
}
else
{
cout<< "插入失败" <<endl;
} pair<map<int,string>::iterator, bool> pair2 = mp.insert(pair<int,string>(,"aaaaa22222"));
if (pair2.second == true)
{
cout<< "插入成功" <<endl;
}
else
{
cout<< "插入失败" <<endl;
}
//方法2.make_pair
mp.insert(make_pair<int,string>(,"bbbbb33333"));
mp.insert(make_pair<int,string>(,"bbbbb44444")); //方法3.value_type
mp.insert(map<int, string>::value_type(,"ccccc55555"));
mp.insert(map<int, string>::value_type(,"ccccc66666")); //方法4.[]
mp[] = "ddddd77777";
mp[] = "ddddd88888"; for (map<int,string>::iterator it = mp.begin(); it != mp.end(); it++)
{
cout<< it->first << "\t" << it->second <<endl;
}
cout<< "--------------------------------" <<endl;
//删除
while(!mp.empty())
{
map<int,string>::iterator it = mp.begin();
cout<< it->first << "\t" << it->second <<endl;
mp.erase(it);
} return ;
}

stl中map的四种插入方法总结的更多相关文章

  1. js中this的四种使用方法

    0x00:js中this的四种调用模式 1,方法调用模式 2,函数调用模式 3,构造器调用模式 4,apply.call.bind调用模式 0x01:第一种:方法调用模式 (也就是用.调用的)this ...

  2. Java中Map的三种遍历方法

    Map的三种遍历方法: 1. 使用keySet遍历,while循环: 2. 使用entrySet遍历,while循环: 3. 使用for循环遍历.   告诉您们一个小秘密: (下↓面是测试代码,最爱看 ...

  3. sqlserver中数据的四种插入方式

    1.insert into stuInfo(name,stuId) values('李洁','19291727')insert into stuInfo(name,stuId) values('李康' ...

  4. Map的四种遍历方法

    1.取值遍历 for(String key:map.keySet()){ System.out.println("key="+key+"and value=" ...

  5. Map的四种遍历

    //Map的四种遍历方法 public static void main(String[] args) { Map<String, String> map = new HashMap< ...

  6. iOS中常用的四种数据持久化方法简介

    iOS中常用的四种数据持久化方法简介 iOS中的数据持久化方式,基本上有以下四种:属性列表.对象归档.SQLite3和Core Data 1.属性列表涉及到的主要类:NSUserDefaults,一般 ...

  7. AJPFX关于Java中运用数组的四种排序方法

    JAVA中在运用数组进行排序功能时,一般有四种方法:快速排序法.冒泡法.选择排序法.插入排序法.快速排序法主要是运用了Arrays中的一个方法Arrays.sort()实现.冒泡法是运用遍历数组进行比 ...

  8. java 遍历map的四种方法

    16:21:42 Map.entrySet() 这个方法返回的是一个Set<Map.Entry<K,V>>,Map.Entry 是Map中的一个接口,他的用途是表示一个映射项( ...

  9. C语言中返回字符串函数的四种实现方法 2015-05-17 15:00 23人阅读 评论(0) 收藏

    C语言中返回字符串函数的四种实现方法 分类: UNIX/LINUX C/C++ 2010-12-29 02:54 11954人阅读 评论(1) 收藏 举报 语言func存储 有四种方式: 1.使用堆空 ...

随机推荐

  1. 虚拟机安装Linux从零到登陆成功教学

    1.Linux Linux使我们出来windows以外可能接触最多的操作系统了,因为好多超级大的互联网公司,比如阿里等就是用Linux的,所以我们最起码要知道怎样去使用,使用的前提就是我们要有一个这样 ...

  2. C#随机数Random

    一.常用操作 NextDouble():返回0-1.0之间的随机数 Next():返回非负随机数(0-216) Next(i):返回一个小于i的非负随机数 Next(i,j):生成i – j 的随机数 ...

  3. 使用safe-rm替换rm命令,防止误删除

    1.下载safe源码包: wget https://launchpad.net/safe-rm/trunk/0.12/+download/safe-rm-0.12.tar.gz 2.解压safe-rm ...

  4. Dubbo源码分析(6):Code2

    背景 定义解码和编码方法. Code2是Code的升级版本. 类图 问题 DubboCodec的父类已经实现了Code2接口并且DubboCodec没有实现Code2接口,为什么要implement ...

  5. matlat保存矩阵数据

    a=[1 2 3; 4 5 6]; fid = fopen('haha.txt', 'w+');fprintf(fid,'%8.4f %8.3f %d\n', a');fclose(fid); typ ...

  6. LightOJ - 1058 - Parallelogram Counting(数学,计算几何)

    链接: https://vjudge.net/problem/LightOJ-1058 题意: There are n distinct points in the plane, given by t ...

  7. C++问题--Reis连接redisContext *pRedisContext = redisConnectWithTimeout("127.0.0.1", 6379, tv);pRedisContext->errstr返回错误磁盘空间不足

    一.问题 使用C++连接Redis的时候出错,错误String为磁盘空间不足,连接代码如下: //reids默认监听端口6387 ; struct timeval tv; tv.tv_sec = iT ...

  8. 33、shuffle性能优化

    一.shuffle性能优化 1.没有开启consolidation机制的性能低下的原理剖析 2.开启consolidation机制之后对磁盘io性能的提升的原理 spark.shuffle.conso ...

  9. 《挑战30天C++入门极限》对C++递增(增量)运算符重载的思考

        对C++递增(增量)运算符重载的思考 在前面的章节中我们已经接触过递增运算符的重载,那时候我们并没有区分前递增与后递增的差别,在通常情况下我们是分别不出++a与a++的差别的,但的确他们直接是 ...

  10. Windows下基于IIS服务的SSL服务器的配置

    Windows下基于IIS服务的SSL服务器的配置 实验环境 Windows Server 2008 R1(CA) Windows Server 2008 R2(web服务器) Windows 7 x ...