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)的拷贝构造与赋值的更多相关文章

  1. STL——容器(Set & multiset)的默认构造 & 带参构造 & 对象的拷贝构造与赋值

    1. 默认构造 set<int> setInt;              //一个存放int的set容器. set<float> setFloat;          //一 ...

  2. 重点:QObject 的拷贝构造和赋值操作——私有

    QObject 中没有提供一个拷贝构造函数和赋值操作符给外界使用,其实拷贝构造和赋值的操作都是已经声明了的,但是它们被使用了Q_DISABLE_COPY () 宏放在了private区域.因此所有继承 ...

  3. QObject 的拷贝构造和赋值操作

    QOject 中没有提供一个拷贝构造函数和赋值操作符给外界使用,其实拷贝构造和赋值的操作都是已经声明了的,但是它们被使用了Q_DISABLE_COPY () 宏放在了private区域.因此所有继承自 ...

  4. STL容器 -- Map

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

  5. STL:map/multimap用法详解

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

  6. STL之map&multimap使用简介

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

  7. C++基本函数的调用优化(构造、拷贝构造、赋值)

    合理的函数可提升时间和空间的利用率 //Test1.h #include<iostream> using namespace std; struct ST { private: int a ...

  8. STL容器Map

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

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

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

随机推荐

  1. Windows10系统下使用Docker搭建ClickHouse开发环境

    前提 随着现在业务开展,几个业务系统的数据量开始急剧膨胀.之前使用了关系型数据库MySQL进行了一次数据仓库的建模,发现了数据量上来后,大量的JOIN操作在提高了云MySQL的配置后依然有点吃不消,加 ...

  2. SQL注入实战新手教程

    本文章仅用于网络安全交流学习,严禁用于非法用途,否则后果自负 一.如何批量找SQL注入(工具+资源都打包):http://www.liuwx.cn/post-149.html 1.SQL注入点搜索关键 ...

  3. mathtype样式系统使用技巧-通过样式定义来更改方程中的字体

    本教程中,我们主要介绍MathType Desktop的样式系统.演示如何通过更改样式定义来更改方程中的字体.通过样式可以快速轻松地实现我们所需的公式格式,并统一所有公式的样式. 我们以如下公式来作为 ...

  4. DIV滚动条设置添加 CSS滚动条显示与滚动条隐藏

    <!DOCTYPE html> <html> <head> <meta charset="gb2312" /> <title& ...

  5. Codeforces Round #665 (Div. 2) D. Maximum Distributed Tree 题解(贪心+易错)

    题目链接 题目大意 给你一课树,要你给每一条边分权值,每条边的权值大于0,他们的乘积等于k,而且要使得n-1条边1的数量尽可能少,定义 f(u,v)为u到v的边权和求 \(\max \sum_{i=1 ...

  6. C语言讲义——传值、传引用

    传值 值类型在做参数的时候,函数内使用的是实参的副本. 函数执行完毕后,即使函数内对参数做了修改,调用方的参数还是原来的值. #include <stdio.h> // 值调用 void ...

  7. python—数据类型和变量

    在python中,能够直接处理的数据类型和变量有整数.浮点数.字符串.布尔值.空值.变量. 一.整数 1.python可处理任意大小的整数,包括负整数,在程序中的表示方法与在数学中的方法一样.例如:0 ...

  8. ubuntu安装vmware

    安装过程: 首先直接将光盘文件中的tar.gz复制到桌面,解压过程如下 中间遇到的问题: 在执行的过程中一直在回车,需要输入的全为yes,还有一个是what is the location of th ...

  9. Kubernetes中Service的使用

    目录 简介 1. Service资源定义 1.1 Service Type ClusterIP 无头service NodePort sessionAffinity实现源地址session绑定 简介 ...

  10. Nginx配置Https(详细、完整)

    Nginx配置Https(详细.完整) 原文链接:请支持原创 前置条件: 在配置https之前请确保下面的步骤已经完成 服务器已经安装nginx并且通过http可以正常访问 不会安装nginx的可以参 ...