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. nginx开启目录浏览

    使用nginx作为下载站点,开启目录浏览的功能 在/etc/nginx/sites-enabled/default中添加: autoindex on ; autoindex_exact_size of ...

  2. centos下多网卡做bond脚本

    多网卡或者单网卡形式下的网卡bonding #! /bin/sh #获取当前网卡数 ethnum=`lspci | grep Ethernet | wc -l` echo $ethnum #如果网卡数 ...

  3. 寻找cmd的管理员运行

    (快捷键----打开"cmd"----"win+R") 在进行环境配置测试中,有些是需要将cmd用管理员方式来运行才可以,但是,当你在"开始" ...

  4. C++深拷贝与浅拷贝区别

    浅拷贝只是对指针的拷贝,浅拷贝后两个指针指向同一个内存空间: 深拷贝不仅对指针进行拷贝,而且对指针指向的内容进行拷贝,经深拷贝后的指针是指向两个不同地址的指针. 当对一个已知对象进行拷贝时,编译系统会 ...

  5. Word 2013中如何直接调用MathType

    相信有很多用户已经发现在使用Word 2013编辑文档时MathType无法直接调用,但是点击文档中的公式时能够跳出MathType公式编辑窗口,那么这是怎么回事呢?其实,这一问题也不是没有办法解决的 ...

  6. FL studio系列教程(十七):FL Studio走带面板介绍

    FL Studio走带面板主要是用来控制播放.录音以及调整歌曲速度的,除此之外还可以用来选择样本剪辑.下面就来详细地看一下这部分菜单. 1.样本/歌曲模式 样本/歌曲模式主要是用来切换样本和歌曲两种模 ...

  7. C++重复结构题解

    买房子 总时间限制:  1000ms 内存限制:  65536kB 描述 某程序员开始工作,年薪N万,他希望在中关村公馆买一套60平米的房子,现在价格是200万,假设房子价格以每年百分之K增长,并且该 ...

  8. DNS、IP地址、子网掩码和默认网关

    一.DNS服务器 DNS是指:域名服务器(Domain Name Server).在Internet上域名与IP地址之间是一一对应的,域名虽然便于人们记忆,但机器之间只能互相认识IP地址,它们之间的转 ...

  9. 手把手教你使用Vue/React/Angular三大框架开发Pagination分页组件

    DevUI是一支兼具设计视角和工程视角的团队,服务于华为云DevCloud平台和华为内部数个中后台系统,服务于设计师和前端工程师.官方网站:devui.designNg组件库:ng-devui(欢迎S ...

  10. fist-冲刺第二天随笔

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE1 这个作业要求在哪里 https://edu.cnblogs.com/campus/fz ...