-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathunordered_set.cpp
More file actions
53 lines (40 loc) · 1.53 KB
/
Copy pathunordered_set.cpp
File metadata and controls
53 lines (40 loc) · 1.53 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
52
53
//############################################################
// | cafe | http://cafe.naver.com/dremdelover |
// | Q&A | https://open.kakao.com/o/gX0WnTCf |
// | business | ultrasuperrok@gmail.com |
//############################################################
#include <iostream>
#include <unordered_set>
using namespace std;
// unordered_set은 해시 테이블을 기반으로 한 STL 컨테이너입니다.
// - 모든 요소는 유일합니다.
// - 내부적인 순서가 정의되어 있지 않습니다.
// 좋은 사용 시기:
// - 중복 없이 요소를 저장하고 싶을 때.
// - 내부 정렬 순서가 중요하지 않을 때.
int main() {
// unordered_set 선언
unordered_set<int> us;
// insert: O(1) 평균, O(n) 최악의 경우 (해시 충돌 시)
us.insert(3);
us.insert(1);
us.insert(4);
us.insert(1); // 중복된 값은 추가되지 않습니다.
// find: O(1) 평균, O(n) 최악의 경우
auto it = us.find(3);
if(it != us.end()) {
cout << "Found: " << *it << endl; // 출력: Found: 3
}
// erase: O(1) 평균, O(n) 최악의 경우
us.erase(1);
// size: O(1)
cout << us.size() << endl; // 출력: 2
// 순회 (정의된 순서가 없습니다): O(n)
for(int num : us) {
cout << num << " "; // 출력: 3 4 (또는 4 3, 순서는 보장되지 않습니다.)
}
cout << endl;
// clear: O(n)
us.clear();
return 0;
}