cpp_unordered_map - 8BitsCoding/RobotMentor GitHub Wiki
- (์ค์) map๊ณผ unordered_map์ ์ฐจ์ด์ ์ map์ ์ ๋ ฌ์ ํ๊ณ (์ด์งํธ๋ฆฌ์ ๋ฐ๋ผ์) unordered_map์ hash map์ ๋ฐ๋ผ ์ ๋ ฌ์ ํ๋ค๋ ์ .
- (์ค์2) ๋ค์ ๋งํด์ unordered_map์ด๋ผ๊ณ ํด๋ ๋ฃ๋๋ฐ๋ก ์ ์ฅ๋๋๊ฒ ์๋๋ผ๋ ๋ง์ด๋ค
๊ทธ๋ฅ map์ ๋ณด์
#include <iosream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> scores;
scores["nono"] = 60;
scores["Mocha"] = 70;
scores["coco"] = 100;
for(auto it = scores.begin(); it != scores.end(); ++it)
{
std::cout << it->first << " : " << it->second << std::endl;
}
// ์ ๋ ฌ์ด ๋ ์ํ๋ก ๋ํ๋๋ค.
return 0;
}
#include <iosream>
#include <unordered_map>
#include <string>
int main() {
std::unordered_map<std::string, int> scores;
scores["nono"] = 60;
scores["Mocha"] = 70;
scores["coco"] = 100;
for(auto it = scores.begin(); it != scores.end(); ++it)
{
std::cout << it->first << " : " << it->second << std::endl;
}
return 0;
}
#include <iostream>
#include <unordered_map>
using namespace std;
int main() {
// your code goes here
unordered_map<int, int> my_map;
my_map.insert(pair<int, int>(5, 1));
my_map.insert(pair<int, int>(4, 1));
unordered_map<int, int>::iterator it = my_map.begin();
for(; it != my_map.end(); it++) {
cout << it->first << endl;
}
return 0;
}
์ถ๋ ฅ
4
5
#include <iosream>
#include <unordered_map>
#include <string>
int main() {
std::unordered_map<std::string, int> scores;
scores["nono"] = 60;
scores["Mocha"] = 70;
scores["coco"] = 100;
scores["ari"] = 40;
scores["chris"] = 90;
for(size_t i = 0; scores.bucket_count(); ++i)
{
std::cout << "Bucket #" << i << ": ";
for(auto it = scores.begin(i); it != scores.end(i); ++it)
{
std::cout << " " << it->first << ":" << it->second;
}
std::cout << std::endl;
}
/*
Bucket #0:
Bucket #1: Ari:40 CoCo:100 NaNa60
Bucket #2:
Bucket #3:
Bucket #4:
Bucket #5: chris:90
Bucket #6: mocha:70
Bucket #7:
*/
// ์ค๋ณต๋๊ฒ ์ ์ฅ์ด ๋๊ธฐ๋ ํ๋ค.
return 0;
}
- ์๋์ผ๋ก ์ ๋ ฌ๋๋ ์ปจํ ์ด๋
- ํค-๊ฐ ์๋ค์ ์ ์ฅ
- ์ด์ง ํ์ ํธ๋ฆฌ
- ํ์์๊ฐ O(logn)
- ์ฝ์ ๊ณผ ์ ๊ฑฐ๊ฐ ๋น๋ฒํ๋ฉด ๋๋ฆฌ๋ค
- ์๋์ผ๋ก ์ ๋ ฌ๋์ง ์๋ ์ปจํ ์ด๋
- ํค-๊ฐ์ ์๋ค ์ ์ฅ
- ํด์ฌ ํ ์ด๋ธ
- ํ์์๊ฐ :
- O(1) ํด์ฌ ์ถฉ๋์ด ์๋ ๊ฒฝ์ฐ
- O(n) ์ต์ ์ ๊ฒฝ์ฐ
- ๋ฒํท ๋๋ฌธ์ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ์ฆ๊ฐ