set(STL)

set的性质

Sets(组) are a type of associative container(容器) in which each element(元素) has to be unique(独特) because the value of the element(元素) identifies(识别) it. The values are stored in a specific(特定) sorted order i.e. either ascending or descending(下降). 解释set不会有重复的东西,即使插入多个重复的东西,那么仍然只会显示一个,而且是有顺序的。

举一个例子来看STL中的函数

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// C++ program to demonstrate various functions of
// STL
#include <iostream>
#include <iterator>
#include <set>
using namespace std;

int main()
{
// empty set container
set<int, greater<int> > s1;//说明这个是大的在前

// insert elements in random order
s1.insert(40);//插入函数
s1.insert(30);
s1.insert(60);
s1.insert(20);
s1.insert(50);

// only one 50 will be added to the set
s1.insert(50);
s1.insert(10);

// printing set s1
set<int, greater<int> >::iterator itr;//迭代器
cout << "\nThe set s1 is : \n";
for (itr = s1.begin(); itr != s1.end(); itr++) {
cout << *itr << " ";
}
cout << endl;

// assigning the elements from s1 to s2
set<int> s2(s1.begin(), s1.end());//默认从小到大

// print all elements of the set s2
cout << "\nThe set s2 after assign from s1 is : \n";
for (itr = s2.begin(); itr != s2.end(); itr++) {
cout << *itr << " ";
}
cout << endl;

// remove all elements up to 30 in s2
cout << "\ns2 after removal of elements less than 30 "
":\n";
s2.erase(s2.begin(), s2.find(30));//删去小于三十的
for (itr = s2.begin(); itr != s2.end(); itr++) {
cout << *itr << " ";
}

// remove element with value 50 in s2
int num;
num = s2.erase(50);
cout << "\ns2.erase(50) : ";
cout << num << " removed\n";
for (itr = s2.begin(); itr != s2.end(); itr++) {
cout << *itr << " ";
}

cout << endl;

// lower bound and upper bound for set s1
cout << "s1.lower_bound(40) : "
<< *s1.lower_bound(40) << endl;
// lower_bound的指的是<=是可以取到
//upper_bound指的是>是只能取到你想要的更高的一位
cout << "s1.upper_bound(40) : "
<< *s1.upper_bound(40) << endl;

// lower bound and upper bound for set s2
cout << "s2.lower_bound(40) : "
<< *s2.lower_bound(40) << endl;
cout << "s2.upper_bound(40) : "
<< *s2.upper_bound(40) << endl;

return 0;
}

结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
The set s1 is : 
60 50 40 30 20 10

The set s2 after assign from s1 is :
10 20 30 40 50 60

s2 after removal of elements less than 30 :
30 40 50 60
s2.erase(50) : 1 removed
30 40 60
s1.lower_bound(40) : 40
s1.upper_bound(40) : 30
s2.lower_bound(40) : 40
s2.upper_bound(40) : 60

例题来熟悉

给定一个大小为n的整型数组a,你需要对其按照升序排序并进行去重

输入

第一行:一个整数n。(1≤n≤2×10^5)

第二行:n个整数,表示数组a的所有元素。(−10^9≤ai​≤10^9),(1≤i≤n)

输出

共一行,输出升序排序且去重后的数组a。

样例输入
1
2
8
2 0 2 3 0 7 1 7
样例输出
1
0 1 2 3 7

ANSWER

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <algorithm>

#include <cmath>

#include <cstring>

#include <iostream>

#include <map>

#include <queue>

#include <set>

#include <stack>

#include <string>

#include <vector>

using namespace std;

typedef long long ll;

constexpr int N = 1e5 + 10;

void solve() {

int n;

cin >> n;

set<int> st;

for (int i = 0; i < n; i++) {

int x;

cin >> x;

st.insert(x);

}

for (auto& v : st) {

cout << v << ' ';

}

}

int main() {

ios::sync_with_stdio(0);

cin.tie(0);

cout.tie(0);

int a = 1;

while (a--) {

solve();

}

}

set(STL)
https://ljw030710.github.io/2023/10/30/set-STL/
Author
iolzyy
Posted on
October 30, 2023
Licensed under