-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteach_c++DS_set.cpp
More file actions
51 lines (48 loc) · 1.13 KB
/
teach_c++DS_set.cpp
File metadata and controls
51 lines (48 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//关联性容器:set 集合
//底层数据结构 RBTree 红黑树
//key和value 一般是映关系
//set 集合里面 key就是vaule(相当于y(数据轴)=x(位置轴))
//不允许重复值,所有元素自动排序,不允许通过迭代器修改数据
#include<set>
#include<iostream>
#include<string>
using namespace std;
int main()
{
set<int> test1;
test1.insert(5);
test1.insert(7);
test1.insert(2);
test1.insert(3);
test1.insert(5);//重复元素不插入
for (set<int>::iterator it = test1.begin(); it != test1.end(); it++)//会自动排序
{
cout << *it << " ";
}
cout << endl;
set<int>::iterator iter = test1.find(7);
test1.erase(iter);
test1.erase(2);
for (set<int>::iterator it = test1.begin(); it != test1.end(); it++)//会自动排序
{
cout << *it << " ";
}
cout << endl;
set<string> test2;
test2.insert("hello");
test2.insert("world");
for (set<string>::iterator it = test2.begin(); it != test2.end(); it++)//会按照字符串strcmp方式排序
{
cout << *it << " ";
}
cout << endl;
multiset<int> test3;//可以重复
test3.insert(3);
test3.insert(3);
test3.insert(2);
for (set<int>::iterator it = test3.begin(); it != test3.end(); it++)//会自动排序
{
cout << *it << " ";
}
cout << endl;
}