본문 바로가기
CS

해시

by ddanss 2024. 1. 31.
728x90

unordered_set

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	unordered_set<int> s;
	s.insert(-50);
	s.insert(200);
	s.insert(20);
	cout << s.erase(200) << '\n'; //1
	cout << s.erase(30) << '\n'; //0
	if (s.find(20) != s.end()) cout << "20이 있음\n";
	else cout << "20이없음\n";

	cout << s.size() << '\n'; //2
	cout << s.count(30) << '\n'; //0 없어서0
	for (auto e : s) cout << e << ' '; // -50 20
}

 

 

unordered_multiset

- 중복허용

- 그냥 erase하면 erase안에 넣은 값 다지워짐

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	unordered_multiset<int> ms;
	ms.insert(-10);
	ms.insert(200);
	ms.insert(20);
	ms.insert(-10);
	ms.insert(20);
	cout << ms.size() << '\n';//5
	for (auto e : ms) cout << e << ' '; //-10 -10 200 20 20
	cout << '\n';
	cout << ms.erase(20) << '\n'; //2
	ms.erase(ms.find(-10)); //-10 1개만 지우기
	ms.insert(200);
	cout << ms.count(200) << '\n'; //2
}

 

 

unordered_map

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	unordered_map<string, int> m;
	m["a"] = 123;
	m["b"] = 456;
	m["cd"] = 789;
	cout << m.size() << '\n'; //3
	m["a"] = 321;
	if (m.find("a") != m.end()) cout << "a가 m에 있음\n"; //있음
	else cout << "a가 m에 없음\n";

	m.erase("b");
	for (auto e : m) cout << e.first << ' ' << e.second << '\n';
	//a 321
	//cd 789
}
반응형

'CS' 카테고리의 다른 글

c++ <-> JAVA 자료구조.  (0) 2024.01.31
다익스트라 vs 플로이드 와샬  (0) 2023.04.28
매개변수탐색  (0) 2023.04.10
이분탐색  (0) 2023.04.09
다익스트라 c++  (1) 2023.04.03

댓글