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 }

打印结果:

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

STL——容器(Set & multiset)的默认构造 & 带参构造 & 对象的拷贝构造与赋值的更多相关文章

  1. STL—— 容器(vector)的内存分配,声明时的普通构造&带参构造

    vector 的几种带参构造 & 初始化与内存分配: 1. 普通的带参构造: vector 的相关对象可以在声明时通过 vector 的带参构造函数进行内存分配,如下: 1 #include ...

  2. STL Set和multiset 容器

    STL Set和multiset 容器 set/multiset的简介 set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列. 元素插入过程是按排序规则插入,所以不能指定插入位 ...

  3. STL - set和multiset

    set/multiset的简介 set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列.元素插入过程是按排序规则插入,所以不能指定插入位置. set采用红黑树变体的数据结构实现, ...

  4. 【转】c++中Vector等STL容器的自定义排序

    如果要自己定义STL容器的元素类最好满足STL容器对元素的要求    必须要求:     1.Copy构造函数     2.赋值=操作符     3.能够销毁对象的析构函数    另外:     1. ...

  5. C++ 构造函数、析构函数、拷贝构造、赋值运算符

    之所以要把它们放在一起,是因为在使用C/C++类语言的时候,很容易混淆这几个概念(对Java来说完全没有这样的问题,表示Javaor完全没有压力). 先建立一个测试类(包含.h和.cpp) //~ P ...

  6. Android(java)学习笔记107-1:通过反射获得带参构造方法并且使用

    反射获得带参构造方法并且使用: 1. 获取字节码文件对象       Class c = Class.forName("cn.itcast_01.Person"); 2.获取带参构 ...

  7. Android(java)学习笔记48:通过反射获得带参构造方法并且使用

    1. 反射获得带参构造方法并且使用: (1)获取字节码文件对象       Class c = Class.forName("cn.itcast_01.Person"); (2)获 ...

  8. STL——容器(Set & multiset)编译器提供的16种构造(挖个坑)

    Set & multiset 在vs2019编译器中提供了16种构造方法 1.默认的无参构造 2.比较容器内容,key_comp()函数返回一个比较key的函数. 3.使用迭代器的区间拷贝,拷 ...

  9. STL容器(Stack, Queue, List, Vector, Deque, Priority_Queue, Map, Pair, Set, Multiset, Multimap)

    一.Stack(栈) 这个没啥好说的,就是后进先出的一个容器. 基本操作有: stack<int>q; q.push(); //入栈 q.pop(); //出栈 q.top(); //返回 ...

随机推荐

  1. BT下载器Folx标签功能怎么实现自动的资源分类

    很多经典的电影作品,比如魔戒三部曲.蜘蛛侠系列.漫威动画系列等,在一个系列中都会包含多个作品.如果使用Folx bt种子下载器自带的电影标签的话,会将这些系列电影都归为"电影"标签 ...

  2. 粉丝少的UP主如何赚大钱

    常逛B站的小伙伴应该知道,B站官方经常会推出各类征稿活动,奖金池也非常高,少则几万,多则上百万,可以说非常受UP主们的欢迎. 图1:B站各类活动 要知道,除了少数头部UP主可能因为没(有)有(钱)看( ...

  3. FL Studio中如何制作和混音警报声

    警报声在当今的许多电影配乐中,或者电子音乐的环境fx中经常出现.为了使用这种尖刺的警示声音,我们除了自己录制已有的警报声以外,也可以使用FL Studio20中的合成器和混音插件来制作属于自己的警报声 ...

  4. FL Studio录制面板作用介绍

    在上一节教程中我们详细的讲解了一下FL Studio录制面板菜单的一些功能,今天我们将继续讲解该面板的知识.具体内容小编这里就不多说了,还是一起来看看吧! 1.录音倒数.该按钮在打开的情况下会在录音前 ...

  5. guitar pro系列教程(二十四):Guitar Pro 7 中文界面的介绍

    用过Guitar Pro这款软件的小伙伴们都知道,Guitar Pro这款吉他软件因为是国外开发商研发的,所以软件最初都是英文版本,对于国内的的吉他爱好者来说,在软件使用上还是很不方便的.随着Guit ...

  6. spring中的事务传播机制

    1.事务的实现思想 在spring中要想某个方法具有事务,只要在方法前加一个@Transactional注解.然后spring就会利用aop思想,在这个方法执行前开启事务, 在方法执行后选择提交事务或 ...

  7. 你还在 if...else?代码这样写才好看!

    前言 if...else 是所有高级编程语言都有的必备功能.但现实中的代码往往存在着过多的 if...else.虽然 if...else 是必须的,但滥用 if...else 会对代码的可读性.可维护 ...

  8. Java集合【2】--iterator接口详解

    目录 一.iterator接口介绍 二.为什么需要iterator接口 三.iterator接口相关接口 3.1 ListIterator 3.2 SpitIterator 3.2.1 SpitIte ...

  9. jvm系列(一)运行时数据区

    C++程序员肩负着每一个对象生命周期开始到终结的维护责任.Java程序员则可以借助自动内存管理机制,不需要自己手动去释放内存.由虚拟机进行内存管理,不容易出现内存泄漏和内存溢出的问题,但是一旦出现这些 ...

  10. 发现了一个关于 gin 1.3.0 框架的 bug

    gin 1.3.0 框架 http 响应数据错乱问题排查 问题概述 客户端同时发起多个http请求,gin接受到请求后,其中一个接口响应内容为空,另外一个接口响应内容包含接口1,接口2的响应内容,导致 ...