STL——容器(Map & multimap)的拷贝构造与赋值
1. Map & multimap 的拷贝构造与赋值
map(const map &mp); //拷贝构造函数
map& operator=(const map &mp); //重载等号操作符
map.swap(mp); //交换两个集合容器
拷贝构造代码示例:
1 #include <iostream>
2 #include <map>
3
4 using namespace std;
5
6 int main()
7 {
8 map<int, string> mapStu1; //默认为升序,与 map<int, string, less<int>> mapStu1; 同效
9
10 mapStu1.insert(pair<int, string>(1, "内容A"));
11 mapStu1.insert(pair<int, string>(2, "内容B"));
12 mapStu1.insert(pair<int, string>(3, "内容C"));
13 mapStu1.insert(pair<int, string>(4, "内容D"));
14
15 map<int, string>mapStu2(mapStu1); //拷贝构造,此时 mapStu2 与 mapStu1 中元素一致
16
17 for (map<int, string>::iterator it = mapStu1.begin(); it != mapStu1.end(); it++) //mapStu1的遍历
18 {
19 cout << "mapStu1的第 " << it->first << "个参数为: " << it->second <<endl;
20 }
21 cout << endl;
22 for (map<int, string>::iterator it = mapStu2.begin(); it != mapStu2.end(); it++) //mapStu2的遍历
23 {
24 cout << "mapStu2的第 " << it->first << "个参数为: " << it->second << endl;
25 }
26
27 return 0;
28 }
打印结果:

重载等号操作代码示例:
1 #include <iostream>
2 #include <map>
3
4 using namespace std;
5
6 int main()
7 {
8 map<int, string> mapStu1; //默认为升序,与 map<int, string, less<int>> mapStu1; 同效
9
10 mapStu1.insert(pair<int, string>(1, "内容A"));
11 mapStu1.insert(pair<int, string>(2, "内容B"));
12 mapStu1.insert(pair<int, string>(3, "内容C"));
13 mapStu1.insert(pair<int, string>(4, "内容D"));
14
15 map<int, string>mapStu2;
16 /*
17 源代码已经实现等号重载,可以直接使用等号赋值
18 map& operator=(const map& _Right){
19 _Mybase::operator=(_Right);
20 return *this;
21 }
22 */
23 mapStu2 = mapStu1;
24
25 for (map<int, string>::iterator it = mapStu1.begin(); it != mapStu1.end(); it++) //mapStu1的遍历
26 {
27 cout << "mapStu1的第 " << it->first << "个参数为: " << it->second <<endl;
28 }
29 cout << endl;
30 for (map<int, string>::iterator it = mapStu2.begin(); it != mapStu2.end(); it++) //mapStu2的遍历
31 {
32 cout << "mapStu2的第 " << it->first << "个参数为: " << it->second << endl;
33 }
34
35 return 0;
36 }
打印结果:

交换容器内容代码示例:
1 #include <iostream>
2 #include <map>
3
4 using namespace std;
5
6 int main()
7 {
8 map<int, string> mapStu1;
9 map<int, string> mapStu2;
10
11 mapStu1.insert(pair<int, string>(1, "内容A"));
12 mapStu1.insert(pair<int, string>(2, "内容B"));
13 mapStu1.insert(pair<int, string>(3, "内容C"));
14 mapStu1.insert(pair<int, string>(4, "内容D"));
15
16 mapStu2.insert(pair<int, string>(5, "内容E"));
17 mapStu2.insert(pair<int, string>(6, "内容F"));
18 mapStu2.insert(pair<int, string>(7, "内容G"));
19 mapStu2.insert(pair<int, string>(8, "内容H"));
20
21 cout << "交换前打印:" << endl;
22 for (map<int, string>::iterator it = mapStu1.begin(); it != mapStu1.end(); it++) //mapStu1的遍历
23 {
24 cout << "mapStu1的第 " << it->first << "个参数为: " << it->second <<endl;
25 }
26 cout << endl;
27 for (map<int, string>::iterator it = mapStu2.begin(); it != mapStu2.end(); it++) //mapStu2的遍历
28 {
29 cout << "mapStu2的第 " << it->first << "个参数为: " << it->second << endl;
30 }
31
32 mapStu1.swap(mapStu2);
33 cout << endl << "交换后打印:" << endl;
34 for (map<int, string>::iterator it = mapStu1.begin(); it != mapStu1.end(); it++) //mapStu1的遍历
35 {
36 cout << "mapStu1的第 " << it->first << "个参数为: " << it->second << endl;
37 }
38 cout << endl;
39 for (map<int, string>::iterator it = mapStu2.begin(); it != mapStu2.end(); it++) //mapStu2的遍历
40 {
41 cout << "mapStu2的第 " << it->first << "个参数为: " << it->second << endl;
42 }
43
44 return 0;
45 }
打印结果:

========================================================================================================================
STL——容器(Map & multimap)的拷贝构造与赋值的更多相关文章
- STL——容器(Set & multiset)的默认构造 & 带参构造 & 对象的拷贝构造与赋值
1. 默认构造 set<int> setInt; //一个存放int的set容器. set<float> setFloat; //一 ...
- 重点:QObject 的拷贝构造和赋值操作——私有
QObject 中没有提供一个拷贝构造函数和赋值操作符给外界使用,其实拷贝构造和赋值的操作都是已经声明了的,但是它们被使用了Q_DISABLE_COPY () 宏放在了private区域.因此所有继承 ...
- QObject 的拷贝构造和赋值操作
QOject 中没有提供一个拷贝构造函数和赋值操作符给外界使用,其实拷贝构造和赋值的操作都是已经声明了的,但是它们被使用了Q_DISABLE_COPY () 宏放在了private区域.因此所有继承自 ...
- STL容器 -- Map
核心描述: map 就是从键(key) 到 值(value) 的一个映射.且键值不可重复,内部按照键值排序. 头文件: #include <map> 拓展: multimap 是一个多重映 ...
- STL:map/multimap用法详解
map/multimap 使用map/multimap之前要加入头文件#include<map>,map和multimap将key/value当作元素,进行管理.它们可根据key的排序准则 ...
- STL之map&multimap使用简介
map 1.insert 第一种:用insert函数插入pair数据 #include <map> #include <string> #include <iostrea ...
- C++基本函数的调用优化(构造、拷贝构造、赋值)
合理的函数可提升时间和空间的利用率 //Test1.h #include<iostream> using namespace std; struct ST { private: int a ...
- STL容器Map
Map的常见函数 Map的实现机制 STL中的Map底层实现机制是RB树(红-黑树)
- 【STL】-Map/Multimap的用法
初始化: map<string,double> salaries; 算法: 1. 赋值.salaries[ "Pat" ] = 75000.00; 2. 无效的索引将自 ...
随机推荐
- Spark3.0.1各种集群模式搭建
对于spark前来围观的小伙伴应该都有所了解,也是现在比较流行的计算框架,基本上是有点规模的公司标配,所以如果有时间也可以补一下短板. 简单来说Spark作为准实时大数据计算引擎,Spark的运行需要 ...
- c#多播委托判断空值
int resualt = (int)fn?.Invoke(a, b); 其中fn是有两个参数一个int返回值的多播委托,这个看起来很精炼,大概意思也不懂 个人理解为使用先判断fn?,再传值以及输出为 ...
- 面试阿里,字节跳动90%会被问到的Java异常面试题集,史上最全系列!
Java异常架构与异常关键字 Java异常简介 Java异常是Java提供的一种识别及响应错误的一致性机制. Java异常机制可以使程序中异常处理代码和正常业务代码分离,保证程序代码更加优雅,并提高程 ...
- Elements-of-Python01_Input/Output
(内容包括输入input,输出print,注释comment,预测类型转换eval,命名与赋值Name & Bestow) 输入Input 和 输出Print Python中利用input() ...
- request封装
request封装 import requests class RequestsHandler: def __init__(self): self.session = requests.Session ...
- Python JSON存储数据
前言: 很多程序都要求用户输入某种信息,如让用户存储游戏首选项或提供要可视化的数据.不管专注的是什么,程序都把用户提供的信息存储在列表和字典等数据结构中.用户关闭 程序时,你几乎总是要保存他们提供的信 ...
- So Easy! HDU - 4565
易知,有\(S_n = \lceil{a + \sqrt{b}}\rceil ^ n\) \(\because a ^ 2 - 1 < b < a ^ 2\) \(\therefore a ...
- kafka 消息存储分析
kafka 可以支持海量数据发送,轻轻松松QPS过十万,如果JVM内存存储这一块如果不够优秀,根本无法支持这么庞大的QPS. 存储架构(这里这是落地数据,并木有涉及到序列化发送数据到broker) R ...
- 【JVM】jdk1.8移除方法区与metaspace
转载:https://blog.csdn.net/aa747604141/article/details/52673582 https://www.jianshu.com/p/a6f19189ec62
- Verilog 分频器
verilog设计进阶 时间:2014年5月6日星期二 主要收获: 1. 自己动手写了第一个verilog程序. 题目: 利用10M的时钟,设计一个单周期形状如下的周期波形. 思考: 最开始的想法是: ...