๐Ÿ“ Coding Test Study/C++

[C++] ํ•ด์‹œ๋งต(Hash Map)์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž (std::unordered_map)

ibelieveinme 2021. 8. 27. 02:03
728x90

[๋ณธ๋ก ์œผ๋กœ ๋“ค์–ด๊ฐ€๊ธฐ ์ „์—...]

๊ธฐ์กด์˜ 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

 

std::unordered_map - cppreference.com

(1) (since C++11) namespace pmr {     template ,               class Pred = std::equal_to >     using unordered_map = std::unordered_map >>; } (2) (since C++17) Unordered map is an associative container that contains key-value pairs with unique

en.cppreference.com

 

728x90