1. 容器(Map & multimap)的插入

map.insert(...);    //往容器插入元素,返回pair<iterator,bool>

map中插入元素的四种方式:

 1 #include <iostream>
2 #include <map>
3
4 using namespace std;
5
6 int main()
7 {
8 map<int, string> mapStu;
9
10 //方法一: 构造一个 pair 然后插入
11 mapStu.insert(pair<int, string>(1, "内容A"));
12
13 //方法二: 使用 make_pair
14 mapStu.insert(make_pair(2, "内容B"));
15
16 //方法三: 使用 value_type, 相当于 pair<int,string>
17 mapStu.insert(map<int, string>::value_type(3, "内容C"));
18
19 //方法四: 使用下标插入,这种方法非常直观,但碰到相同的 key 时会进行覆盖操作
20 mapStu[4] = "内容D";
21
22 //只要创建了 key ,这种情况会构造创建内存,生成 string 的默认空值,创建完右值后,拷贝给左值
23 mapStu[6] = mapStu[5];
24
25 //将 key 为4的内容拷贝至7
26 mapStu[7] = mapStu[4];
27
28 for (map<int, string>::iterator it = mapStu.begin(); it != mapStu.end(); it++)
29 {
30 cout << "key: " << (*it).first << " value: " << (*it).second << endl;
31 }
32
33 return 0;
34 }

打印结果:

1. 前三种方法,采用的是insert()方法,该方法返回值为pair<iterator, bool>

2. 第四种方法非常直观,但碰到相同的键时会进行覆盖操作。比如插入key 为4的键值时,先在mapStu中查找主键为4的项,若不存在,则将一个键为4,值为默认初始化值的对组插入到 mapStu 中,然后再将值修改成“内容D”。若发现已存在4这个键,则修改这个键对应的value。

3. string strName = mapStu[8];   //取值操作或插入操作

4. 只有当mapStu存在8这个键时才是正确的取操作,否则会自动插入一个实例,键为8,值为默认构造时的初始化值。

5. 在 key 值存在的情况下, 只有通过下标方式可以覆盖修改 key 对应的value (插入成功), 通过 insert 方法则不会(插入失败).

2. 使用 insert 插入返回 pair<iterator,bool>

下边例子用返回的打印判断插入是否成功:

 1 #include <iostream>
2 #include <map>
3
4 using namespace std;
5
6 int main()
7 {
8 map<int, string> mapStu;
9
10 //方法一: 构造一个 pair 然后插入,
11 pair <map<int, string>::iterator, bool> ret_1 = mapStu.insert(pair<int, string>(1, "内容A"));
12 if (ret_1.second == true)
13 {
14 cout << "插入" << (*(ret_1.first)).second<< "成功" << endl;
15 }
16 else
17 {
18 cout << "插入" << "内容A新" << "失败" << endl;
19 }
20 pair <map<int, string>::iterator, bool> ret_2 = mapStu.insert(pair<int, string>(1, "内容A新"));
21 if (ret_2.second == true)
22 {
23 cout << "插入" << (*(ret_2.first)).second << "成功" << endl;
24 }
25 else
26 {
27 cout << "插入" << "内容A新" << "失败" << endl;
28 }
29
30 //方法二: 使用 make_pair
31 pair <map<int, string>::iterator, bool> ret_3 = mapStu.insert(make_pair(2, "内容B"));
32 if (ret_3.second == true)
33 {
34 cout << "插入" << (*(ret_3.first)).second << "成功" << endl;
35 }
36 else
37 {
38 cout << "插入" << "内容B新" << "失败" << endl;
39 }
40 pair <map<int, string>::iterator, bool> ret_4 = mapStu.insert(make_pair(2, "内容B新"));
41 if (ret_4.second == true)
42 {
43 cout << "插入" << (*(ret_4.first)).second << "成功" << endl;
44 }
45 else
46 {
47 cout << "插入" << "内容B新" << "失败" << endl;
48 }
49
50 //方法三: 使用 value_type, 相当于 pair<int,string>
51 pair<map<int, string>::iterator, bool> ret_5 = mapStu.insert(map<int, string>::value_type(3, "内容C"));
52 if (ret_5.second == true)
53 {
54 cout << "插入" << (*(ret_5.first)).second << "成功" << endl;
55 }
56 else
57 {
58 cout << "插入" << "内容C新" << "失败" << endl;
59 }
60 pair<map<int, string>::iterator, bool> ret_6 = mapStu.insert(map<int, string>::value_type(3, "内容C新"));
61 if (ret_6.second == true)
62 {
63 cout << "插入" << (*(ret_6.first)).second << "成功" << endl;
64 }
65 else
66 {
67 cout << "插入" << "内容C新" << "失败" << endl;
68 }
69
70 for (map<int, string>::iterator it = mapStu.begin(); it != mapStu.end(); it++)
71 {
72 cout << "key: " << (*it).first << " value: " << (*it).second << endl;
73 }
74
75 return 0;
76 }

打印结果:

3. 容器(Map & multimap)的迭代器

²  map.begin();  //返回容器中第一个数据的迭代器。

²  map.end();  //返回容器中最后一个数据之后的迭代器。

²  map.rbegin();  //返回容器中倒数第一个元素的迭代器。

²  map.rend();   //返回容器中倒数最后一个元素的后面的迭代器。

==========================================================================================================================

STL——容器(Map & multimap)的插入与迭代器的更多相关文章

  1. STL:map/multimap用法详解

    map/multimap 使用map/multimap之前要加入头文件#include<map>,map和multimap将key/value当作元素,进行管理.它们可根据key的排序准则 ...

  2. STL容器 -- Map

    核心描述: map 就是从键(key) 到 值(value) 的一个映射.且键值不可重复,内部按照键值排序. 头文件: #include <map> 拓展: multimap 是一个多重映 ...

  3. STL之map&multimap使用简介

    map 1.insert 第一种:用insert函数插入pair数据 #include <map> #include <string> #include <iostrea ...

  4. STL——容器(Set & multiset)的迭代器

    1.set.insert(elem);     //在容器中插入元素. 2.set.begin();         //返回容器中第一个数据的迭代器. 3.set.end();          / ...

  5. STL容器Map

    Map的常见函数 Map的实现机制 STL中的Map底层实现机制是RB树(红-黑树)

  6. 【STL】-Map/Multimap的用法

    初始化: map<string,double> salaries; 算法: 1. 赋值.salaries[ "Pat" ] = 75000.00; 2. 无效的索引将自 ...

  7. STL - 容器 - Map(二)

    把Map用作关联式数组 MapAdvanceTest.cpp #include <map> #include <string> #include <iostream> ...

  8. STL - 容器 - Map(一)

    MapTest.cpp #include <map> #include <string> #include <iostream> #include <algo ...

  9. iBinary C++STL模板库关联容器之map/multimap

    目录 一丶关联容器map/multimap 容器 二丶代码例子 1.map的三种插入数据的方法 3.map集合的遍历 4.验证map集合数据是否插入成功 5.map数据的查找 6.Map集合删除元素以 ...

  10. STL容器之一vector

    STL中最简单也是最有用的容器之一是vector<T>类模板,称为向量容器,是序列类型容器中的一种. 1.vector<T> 对象的基本用法(1)声明:vector<ty ...

随机推荐

  1. MySQL_where和having的区别

    1. where和having都可以使用的场景 select goods_price,goods_name from sw_goods where goods_price > 100 selec ...

  2. Python input用户交互

    1.input(),阻塞等待用户输入内容并敲回车. 1 #-*- encoding:utf-8 -*- 2 3 name = input('请输入你的名字') 4 5 age = input('请输入 ...

  3. css子选择器 :frist-child :nth-child(n) :nth-of-type(n) ::select选择器

    记录一下前一段时间使用.学习的几种选择器. 1. :frist-child 选择器n 比如<ul><li></li> <li></li> & ...

  4. 状态模式(Established close)

    状态模式(Established close) 引子 铁扇公主:以前陪我看月亮的时候,叫人家小甜甜,现在新人胜旧人了,叫人家牛夫人! 定义 Allow an object to alter its b ...

  5. Linux下查询外网IP的办法。

    Curl 纯文本格式输出:curl icanhazip.comcurl ifconfig.mecurl curlmyip.comcurl ip.appspot.comcurl ipinfo.io/ip ...

  6. [python应用]python简单图片抓取

    前言 emmmm python简单图片抓取 1 import requests 2 import threading 3 import queue 4 from subprocess import P ...

  7. Golang 实现 Redis(6): 实现 pipeline 模式的 redis 客户端

    本文是使用 golang 实现 redis 系列的第六篇, 将介绍如何实现一个 Pipeline 模式的 Redis 客户端. 本文的完整代码在Github:Godis/redis/client 通常 ...

  8. VMware与Device/Credential Guard不兼容问题

    启动虚拟机vmware突然报不兼容错误 解决方法: 1首先打开控制面板>程序>启动或关闭Windows功能, 取消Hyper-v的勾选 2.在往下划,关闭Windows沙盒的勾选沙盒和虚拟 ...

  9. 自学linux——12.shell进阶

    Shell进阶 当把在Windows中写好的脚本传到linux中使用时,在Windows下每一行结尾是\n\r,而Linux下则是\n,所以会多出来\r,在linux中运行脚本时,需执行: sed - ...

  10. python中字符串的编码和解码

    1. 常用的编码 ASCII:只能表示一些字母,数字和特殊的字符,占一个字节 GBK:国家简体中文字符集和繁体字符集,兼容ASCII,占两个字节 Unicode:能够表示全世界上所有的字符,Unico ...