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. 无效的索引将自 ...
随机推荐
- HDU100题简要题解(2030~2039)
HDU2030 汉字统计 题目链接 Problem Description 统计给定文本文件中汉字的个数. Input 输入文件首先包含一个整数n,表示测试实例的个数,然后是n段文本. Output ...
- sqlilab less11-less18
less-11 uname和passwd直接带入查询,万能密码 sqlmap自动搜索表单,或者抓包后用-r参数 less-12 post数据用小括号进行包裹,构造万能密码") or 1=1 ...
- 怎么绘制C语言选择和循环语句的思维导图
C语言是一门非常基础的计算机语言,是大部分本科学生的公共专业,在C语言的学习中,选择和循环语句是至关重要的部分,利用思维导图可以有效节约时间并加深知识点记忆. 接下来就为大家介绍一下我用iMindMa ...
- request封装
request封装 import requests class RequestsHandler: def __init__(self): self.session = requests.Session ...
- 从递归到memo到动态规划
//memo,记忆化搜索 class Solution { int[][] memo; public boolean wordBreak(String s, List<String& ...
- 「刷题笔记」Tarjan
贴一个讲得非常详细的\(tarjan\)入门教程 信息传递 讲个笑话:我之前用并查集求最小环过的这题,然后看见题目上有个\(tarjan\)标签 留下了深刻的印象:\(tarjan\)就是并查集求最小 ...
- java集合源码分析(三):ArrayList
概述 在前文:java集合源码分析(二):List与AbstractList 和 java集合源码分析(一):Collection 与 AbstractCollection 中,我们大致了解了从 Co ...
- 题解 洛谷 P3396 【哈希冲突】(根号分治)
根号分治 前言 本题是一道讲解根号分治思想的论文题(然鹅我并没有找到论文),正 如论文中所说,根号算法--不仅是分块,根号分治利用的思想和分块像 似却又不同,某一篇洛谷日报中说过,分块算法实质上是一种 ...
- 真香!Python开发工程师都选择这个数据库:因为它免费
数据库类别 既然我们要使用关系数据库,就必须选择一个关系数据库. 目前广泛使用的关系数据库也就这么几种: 付费的商用数据库: Oracle,典型的高富帅: SQL Server,微软自家产品,Wind ...
- Python正则表达式re.findall("[A-Za-z]([A-Za-z0-9])*[.]txt",'Abc2019.txt')的结果为什么是['9']
在<Python妙用re.sub分析正则表达式匹配过程>中老猿分析了findall函数的返回情况,老猿前一阵子在执行这个语句时: >>> re.findall(" ...