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. Cephfs的快照功能

    前言 Cephfs的快照功能在官网都很少提及,因为即使开发了很多年,但是由于cephfs的复杂性,功能一直没能达到稳定,这里,只是介绍一下这个功能,怎么使用,并且建议不要在生产中使用,因为搞不好是会丢 ...

  2. tomcat设置好环境变量,依然无法通过cmd startup命令启动

    Windows环境下JDK安装与环境变量配置详细的图文教程 https://www.cnblogs.com/liuhongfeng/p/4177568.html   Windows环境下maven 环 ...

  3. appium 数据参数化 登录模块

    下面是我最近学习的PYTHON的登录代码: class test(object): def getdic(self): d = {'username': '13', 'password': '1111 ...

  4. Java项目读取resources资源文件路径那点事

    今天在Java程序中读取resources资源下的文件,由于对Java结构了解不透彻,遇到很多坑.正常在Java工程中读取某路径下的文件时,可以采用绝对路径和相对路径,绝对路径没什么好说的,相对路径, ...

  5. webug第一关:很简单的一个注入

    第一关:很简单的一个注入 上单引号报错 存在注入,用order  by猜列的个数 union select 出现显示位 查数据库版本,用户和当前数据库名 查表名和列名 最后,激动人心的拿flag

  6. laravel 验证器使用

    1.前后端不分离 (form表单提交) 控制器定义验证规则 <?php namespace App\Http\Controllers\Admin; use Illuminate\Http\Req ...

  7. Model class apps.goods.models.GoodsType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS

    在admin.py注册这个model时,报了个错: RuntimeError: Model class apps.goods.models.GoodsType doesn't declare an e ...

  8. PDF编辑:pdfFactory文本备注功能详解

    除了word的doc文件外,PDF也是我们经常接触到的文件格式,经常需要在pdf文件上进行编辑与修改,或者给内容做提示和备注. 文件的文本备注功能可以用pdfFactory来进行,编辑打印PDF一条龙 ...

  9. 写给程序员的机器学习入门 (九) - 对象识别 RCNN 与 Fast-RCNN

    因为这几个月饭店生意恢复,加上研究 Faster-RCNN 用掉了很多时间,就没有更新博客了.这篇开始会介绍对象识别的模型与实现方法,首先会介绍最简单的 RCNN 与 Fast-RCNN 模型,下一篇 ...

  10. JavaSE 学习笔记03丨继承、接口、多态、内部类

    Chapter. 5 继承 继承作为面向对象的三大特征之一,它是多态的前提.它主要解决的问题是共性抽取. Java中的继承,是单继承.多级继承的. 已存在的类,被称为超类.基类.父类(parent c ...