Fork me on GitHub

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

1. 默认构造

set<int> setInt;              //一个存放int的set容器。

set<float> setFloat;          //一个存放float的set容器。

set<string> setString;         //一个存放string的set容器。

multiset<int> mulsetInt;            //一个存放int的multi set容器。

multiset<float> multisetFloat;       //一个存放float的multi set容器。

multiset<string> multisetString;     //一个存放string的multi set容器。

示例代码:

 1 #include <iostream>
 2 #include <set>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     set<int> setInt;                    //一个存放 int 的 set 容器。
 9     for (int i = 0; i < 5; i++)
10     {
11         setInt.insert(100 - i);
12     }
13     setInt.insert(100);
14     for (set<int>::iterator it = setInt.begin(); it != setInt.end(); it++)
15     {
16         cout << *it << " ";
17     }
18     cout << endl;
19 
20     set<float> setFloat;                //一个存放 float 的 set 容器。
21     for (int i = 0; i < 5; i++)
22     {
23         setFloat.insert((100 - i) * 0.5);
24     }
25     setFloat.insert(50);
26     for (set<float>::iterator itf = setFloat.begin(); itf != setFloat.end(); itf++)
27     {
28         cout << *itf << " ";
29     }
30     cout << endl;
31 
32     set<string> setString;                //一个存放 string 的 set 容器。
33     setString.insert("a");
34     setString.insert("a");
35     setString.insert("a");
36     for (set<string>::iterator its = setString.begin(); its != setString.end(); its++)
37     {
38         cout << *its << " ";
39     }
40     cout << endl;
41 
42 
43     /***************************** multiset ********************************/
44     cout << "*********** multiset *********" << endl;
45     multiset<int> mulsetInt;            //一个存放 int 的 multiset 容器。
46     for (int i = 0; i < 5; i++)
47     {
48         mulsetInt.insert(100 - i);
49     }
50     mulsetInt.insert(100);
51     for (set<int>::iterator it = mulsetInt.begin(); it != mulsetInt.end(); it++)
52     {
53         cout << *it << " ";
54     }
55     cout << endl;
56 
57     multiset<float> multisetFloat;       //一个存放 float 的 multiset 容器。
58     for (int i = 0; i < 5; i++)
59     {
60         multisetFloat.insert((100 - i) * 0.5);
61     }
62     multisetFloat.insert(50);
63     for (set<float>::iterator itf = multisetFloat.begin(); itf != multisetFloat.end(); itf++)
64     {
65         cout << *itf << " ";
66     }
67     cout << endl;
68 
69     multiset<string> multisetString;     //一个存放 string 的 multiset 容器。
70     multisetString.insert("a");
71     multisetString.insert("a");
72     multisetString.insert("a");
73     for (set<string>::iterator its = multisetString.begin(); its != multisetString.end(); its++)
74     {
75         cout << *its << " ";
76     }
77     cout << endl;
78     
79     return 0;
80 }

打印结果:

 

 

2. 带参构造

set(beg,end);       //将[beg, end)区间中的元素拷贝给本身。
multiset(beg,end);       //将[beg, end)区间中的元素拷贝给本身。

 1 #include <iostream>
 2 #include <set>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     set<int> setInt;
 9     for (int i = 0; i < 5; i++)
10     {
11         setInt.insert(100 - i);
12     }
13 
14     set<int> setInt1(setInt.begin(), setInt.end());
15     for (set<int>::iterator it = setInt1.begin(); it != setInt1.end(); it++)
16     {
17         cout << *it << " ";
18     }
19     cout << endl;
20     
21     return 0;
22 }

打印结果:

 

set(const set &s);        //拷贝构造函数。

multiset(const multiset &s);    //拷贝构造函数。

 1 #include <iostream>
 2 #include <set>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     set<int> setInt;
 9     for (int i = 0; i < 5; i++)
10     {
11         setInt.insert(100 - i);
12     }
13 
14     set<int> setInt1(setInt);
15     for (set<int>::iterator it = setInt1.begin(); it != setInt1.end(); it++)
16     {
17         cout << *it << " ";
18     }
19     cout << endl;
20     
21     return 0;
22 }

打印结果

 

 

3. 拷贝构造与赋值

set(const set &st);            //拷贝构造函数

set& operator=(const set &st);      //重载等号操作符,这些都差不多,不过多解释了

 1 #include <iostream>
 2 #include <set>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     set<int> setInt;
 9     for (int i = 0; i < 5; i++)
10     {
11         setInt.insert(100 - i);
12     }
13 
14     // set<int> setInt1(setInt);    //拷贝构造
15     set<int> setInt1 = setInt;        //赋值构造
16     for (set<int>::iterator it = setInt1.begin(); it != setInt1.end(); it++)
17     {
18         cout << *it << " ";
19     }
20     cout << endl;
21     
22     return 0;
23 }

 

set.swap(st);            //交换两个集合容器

 1 #include <iostream>
 2 #include <set>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     set<int> setInt1;
 9     for (int i = 0; i < 5; i++)
10     {
11         setInt1.insert(i);
12     }
13     set<int> setInt2;
14     for (int i = 0; i < 5; i++)
15     {
16         setInt2.insert(i);
17     }
18     setInt2.insert(666);
19 
20     cout << "遍历setInt1" << endl;
21     for (set<int>::iterator it = setInt1.begin(); it != setInt1.end(); it++)
22     {
23         cout << *it << " ";
24     }
25     cout << endl;
26     cout << "遍历setInt2" << endl;
27     for (set<int>::iterator it = setInt2.begin(); it != setInt2.end(); it++)
28     {
29         cout << *it << " ";
30     }
31     cout << endl;
32 
33     cout << endl << "使用 swap 交换两个容器" << endl;
34 
35     setInt1.swap(setInt2);
36     cout << "交换后遍历 setInt1" << endl;
37     for (set<int>::iterator it = setInt1.begin(); it != setInt1.end(); it++)
38     {
39         cout << *it << " ";
40     }
41     cout << endl;
42 
43     cout << "交换后遍历 setInt2" << endl;
44     for (set<int>::iterator it = setInt2.begin(); it != setInt2.end(); it++)
45     {
46         cout << *it << " ";
47     }
48     cout << endl;
49 
50     return 0;
51 }

打印结果:

 

 

 

 

 

 

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

 

posted @ 2020-05-17 22:01  索智源  阅读(356)  评论(0编辑  收藏  举报