Fork me on GitHub

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 }

打印结果:

 

 

 

 

 

 

========================================================================================================================

posted @ 2020-06-15 01:38  索智源  阅读(2160)  评论(0编辑  收藏  举报