[๋ณธ๋ก ์ผ๋ก ๋ค์ด๊ฐ๊ธฐ ์ ์...]
๊ธฐ์กด์ map์ ์์๊ฐ ์๋์ผ๋ก ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ๋๋ ์ด์ง ํ์ ํธ๋ฆฌ(BST) ๊ธฐ๋ฐ์ด์๋ค. ๊ทธ๋์ ์ ๋ ฌ์ด ํ์ํ์ง ์์ ๊ฒฝ์ฐ์๋ ์๋์ผ๋ก ์ ๋ ฌ๋์ด ๋ถํ์ํ ์ค๋ฒํค๋๊ฐ ์์๋ค. ์ฆ, ์ค๋ฒํค๋๋ฅผ ๊ฐ์ํ๋ฉด์ map์ ์ฌ์ฉํ๋์ง, ๋นํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ธ hash_map์ ์ฌ์ฉํด์ผ ํ๋ค.
๊ทธ๋ฐ๋ฐ, C++11 ๋ถํฐ ์ด๋ฐ ๋ถํธํจ์ ํด์ํด์ค unordered_map ์ด ๋ฑ์ฅํ๋ค. map๊ณผ ๋ฌ๋ฆฌ ์ด์ง ํ์ ํธ๋ฆฌ๊ฐ ์๋๋ผ 'ํด์ ํ ์ด๋ธ'๋ก ๊ตฌํ๋์ด ์๋ ์๋ฃ๊ตฌ์กฐ๋ก. unorderd_map์ ์ ๋ ฌ์ด ์๋์ผ๋ก ๋์ง ์๊ณ , ์์ ๊ฒ์/์ฝ์ /์ญ์ ์ฐ์ฐ์ด O(1) ์๊ฐ ์์ ๊ฐ๋ฅํ๋ค. ๊ทธ๋์ ์ฐ๋ฆฌ๊ฐ ๊ณต๋ถํด์ผํ ์๋ฃ๊ตฌ์กฐ๋ unordered_map ์ด๋ค !
[unordered_map(hash_map)์ ํน์ง]
์ฐ์ , map์ key - value ์์ ๊ตฌ์กฐ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ์๋ฃ๊ตฌ์กฐ์ด๋ค.
๊ทธ๋์ key๋ฅผ ํตํด value๋ฅผ ๊ฒ์ํ ์ ์๋ค.
hash_map์ hash ํจ์๋ฅผ ํตํด key๋ฅผ ํน์ ๊ฐ์ผ๋ก ๋ณํ์ํค๊ณ ์ด ๊ฐ์ value๋ฅผ ์ ์ฅํ ๊ณต๊ฐ์ ์ธ๋ฑ์ค๋ก ์ฌ์ฉํ๋ค.
์ด hash_map์ ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก unordered_map์ด ์ฐ์ด๊ณ ์๋ ๊ฑฐ๋ค.
[unordered_map ์ฌ์ฉ๋ฒ]
* ํค๋: <unordered_map>
* ์ ์ธ: unordered_map<์๋ฃํ, ์๋ฃํ> map;
* ์๋ฃ์ฝ์ : map.insert(pair<์๋ฃํ, ์๋ฃํ>(value1, value2)); ํน์ map.insert(make_pair(value1, value2)); ํน์ map[key] = value;
* ์๋ฃ๊ฒ์: map.find(key)->first; map.find(key)->second; ํน์ map[key]; (first๊ฐ key, second๊ฐ value)
* ์๋ฃ์ญ์ : map.erase(key); ํน์ map.erase(map.find(key));
* map ์ฌ์ด์ฆ ๊ตฌํ๊ธฐ: map.size();
* map ๋น์ฐ๊ธฐ: map.empty();
* map ์ด๊ธฐํ: map.clear();
* key์ ํด๋นํ๋ ์์์ ๊ฐ์ ๊ตฌํ๊ธฐ: map.count(key);
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
// Create an unordered_map of three strings (that map to strings)
unordered_map<string, string> u = {
{"RED","#FF0000"},
{"GREEN","#00FF00"},
{"BLUE","#0000FF"}
};
cout << "Iterate and print keys and values of unordered_map, being explicit with\n"
"the type of the iterator, n:\n";
for( const pair<string, string>& n : u ) {
cout << "Key:[" << n.first << "] Value:[" << n.second << "]\n";
}
cout << "Iterate and print keys and values of unordered_map, using auto:\n";
for(const auto& n : u ) {
cout << "Key:[" << n.first << "] Value:[" << n.second << "]\n";
}
cout << "Iterate and print keys and values using structured binding (since C++17):\n";
for(const auto& [key, value] : u ) {
cout << "Key:[" << key << "] Value:[" << value << "]\n";
}
// Add two new entries to the unordered_map
u["BLACK"] = "#000000";
u["WHITE"] = "#FFFFFF";
cout << "Output values by key:\n";
cout << "The HEX of color RED is:[" << u["RED"] << "]\n";
cout << "The HEX of color BLACK is:[" << u["BLACK"] << "]\n";
}
output:
Iterate and print keys and values of unordered_map, being explicit with
the type of the iterator, n:
Key:[BLUE] Value:[#0000FF]
Key:[GREEN] Value:[#00FF00]
Key:[RED] Value:[#FF0000]
Iterate and print keys and values of unordered_map, using auto:
Key:[BLUE] Value:[#0000FF]
Key:[GREEN] Value:[#00FF00]
Key:[RED] Value:[#FF0000]
Iterate and print keys and values using structured binding (since C++17):
Key:[BLUE] Value:[#0000FF]
Key:[GREEN] Value:[#00FF00]
Key:[RED] Value:[#FF0000]
Output values by key:
The HEX of color RED is:[#FF0000]
The HEX of color BLACK is:[#000000]
[C++ ๊ณต์ ๋ ํผ๋ฐ์ค ์ฐธ์กฐ]
https://en.cppreference.com/w/cpp/container/unordered_map