๐Ÿ“Œ C++ STL - map ์ •๋ฆฌ

โœ… map ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•

#include <map>  // ํ—ค๋”ํŒŒ์ผ ํฌํ•จํ•ด์•ผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
 
int main() {
    std::map<int, int> mapInt; 
    
    mapInt.insert(std::make_pair(1, 100)); // ๊ฐ€๋Šฅ (pair ๊ฐ์ฒด ์‚ฌ์šฉ)
    
    for (std::map<int, int>::iterator iter = mapInt.begin(); iter != mapInt.end(); ++iter) {
        std::cout << iter->first << " : " << iter->second << std::endl;
    }
}
  • map์€ pair ๊ฐ์ฒด๋ฅผ ์›์†Œ๋กœ ๊ฐ€์ง€๋ฏ€๋กœ, (*iter).first, (*iter).second ๋˜๋Š” iter->first, iter->second๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•จ.
  • map์€ ์ž๋™์ •๋ ฌ์„ ์ง€์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ •๋ ฌ ๋ฐฉ์‹์— ๋Œ€ํ•œ ์กฐ๊ฑด์ž๋ฅผ ์„ ์–ธ์‹œ์— ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ.

โœ… map ์›์†Œ ์ถ”๊ฐ€ ๋ฐฉ๋ฒ•

1๏ธโƒฃ pair ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•˜์—ฌ ์ถ”๊ฐ€

std::pair<int, int> MyPair(1, 100);
mapInt.insert(MyPair);

2๏ธโƒฃ pair ์ž„์‹œ ๊ฐ์ฒด ์‚ฌ์šฉ

mapInt.insert(std::pair<int, int>(2, 200));

3๏ธโƒฃ make_pair ํ•จ์ˆ˜ ์‚ฌ์šฉ (์ถ”์ฒœ)

mapInt.insert(std::make_pair(3, 300));

4๏ธโƒฃ value_type ์‚ฌ์šฉ

std::map<int, int>::value_type MyValue(4, 400);
mapInt.insert(MyValue);

5๏ธโƒฃ value_type ์ž„์‹œ ๊ฐ์ฒด ์‚ฌ์šฉ

mapInt.insert(std::map<int, int>::value_type(5, 500));

6๏ธโƒฃ [] ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ (์ฃผ์˜!)

mapInt[6] = 600;
  • insert์™€ ์ฐจ์ด์ 
    • insert๋Š” ์ค‘๋ณต๋œ ํ‚ค๊ฐ€ ์žˆ์œผ๋ฉด ์ถ”๊ฐ€๋˜์ง€ ์•Š์Œ.
    • [] ์—ฐ์‚ฐ์ž๋Š” ํ‚ค๊ฐ€ ์กด์žฌํ•˜๋ฉด ๊ธฐ์กด ๊ฐ’์„ ๋ฎ์–ด์”€.

7๏ธโƒฃ ๋ชจ๋˜ C++ (C++11 ์ด์ƒ)

๐Ÿ”น ์œ ๋‹ˆํผ ์ดˆ๊ธฐํ™” ์‚ฌ์šฉ

mapInt.insert({7, 700});

๐Ÿ”น emplace ์‚ฌ์šฉ (์ถ”์ฒœ)

mapInt.emplace(8, 800);
  • insert๋Š” pair ๊ฐ์ฒด๋ฅผ ๋จผ์ € ์ƒ์„ฑํ•œ ํ›„ ์‚ฝ์ž…, emplace๋Š” ์ง์ ‘ ์ƒ์„ฑํ•˜์—ฌ ๋” ํšจ์œจ์ ์ž„.

โœ… map์˜ ๋ฐ˜๋ณต์ž

  • ์–‘๋ฐฉํ–ฅ ๋ฐ˜๋ณต์ž ์ง€์› (++, -- ๊ฐ€๋Šฅ)
std::map<int, int>::iterator iter = mapInt.begin();
iter++;
iter++;
iter--;
 
mapInt.insert({10, 1000}); // ์ž๋™ ์ •๋ ฌ๋˜๋ฏ€๋กœ ์ค‘๊ฐ„ ์‚ฝ์ž…์€ ์˜๋ฏธ ์—†์Œ
 
mapInt.erase(iter);   // ํŠน์ • ์œ„์น˜์˜ ์›์†Œ ์‚ญ์ œ
mapInt.erase(4);      // key ๊ฐ’์œผ๋กœ ์‚ญ์ œ ๊ฐ€๋Šฅ

โœ… map๊ณผ find_if

#include <iostream>
#include <map>
#include <cstring>
#include <algorithm>
 
struct tagFinder {
    tagFinder(const char* pTag) : m_pTag(pTag) {}
    
    template<typename T>
    bool operator()(const T& MyPair) const {
        return !strcmp(m_pTag, MyPair.first);
    }
    
    const char* m_pTag;
};
 
int main() {
    std::map<const char*, int, bool(*)(const char*, const char*)> myMap(
        [](const char* a, const char* b) { return strcmp(a, b) < 0; }
    );
 
    myMap.insert({"aaa", 100});
    myMap.insert({"bbb", 200});
    myMap.insert({"ccc", 300});
 
    // ์ผ๋ฐ˜ find (์ฃผ์†Œ ๋น„๊ต๋กœ ์ธํ•ด ๋น„์ •์ƒ์ ์ธ ๊ฒฐ๊ณผ ๊ฐ€๋Šฅ)
    auto iter = myMap.find("bbb");
 
    // find_if๋ฅผ ์‚ฌ์šฉํ•œ ๋ฌธ์ž์—ด ๋น„๊ต
    iter = std::find_if(myMap.begin(), myMap.end(), tagFinder("bbb"));
 
    if (iter != myMap.end()) {
        std::cout << "์ฐพ์€ ๊ฐ’: " << iter->second << std::endl;
    } else {
        std::cout << "์ฐพ์ง€ ๋ชปํ•จ\n";
    }
    return 0;
}

๐Ÿšจ map<const char*, int>์˜ ๋ฌธ์ œ์ 

  • const char*์„ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์ฃผ์†Œ ๊ฐ’์œผ๋กœ ๋น„๊ตํ•˜๋ฏ€๋กœ "bbb"๋ฅผ ์ œ๋Œ€๋กœ ์ฐพ์ง€ ๋ชปํ•  ์ˆ˜๋„ ์žˆ์Œ.
  • ํ•ด๊ฒฐ๋ฒ•:
    1. std::string์„ ํ‚ค๋กœ ์‚ฌ์šฉ (๊ถŒ์žฅ)
    2. strcmp๋ฅผ ์ด์šฉํ•œ ๋น„๊ต์ž ์ถ”๊ฐ€

โœ… map์˜ ์ •๋ ฌ ๋ฐฉ์‹

Key ํƒ€์ž…์ •๋ ฌ ๊ธฐ์ค€
charO (ASCII ์ฝ”๋“œ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ)
char*X (์ฃผ์†Œ ๊ฐ’ ๊ธฐ์ค€ ์ •๋ ฌ)
stringO (operator<๊ฐ€ ์•ŒํŒŒ๋ฒณ ๊ธฐ์ค€ ์ •๋ ฌ)

๐Ÿ“Œ ์ •๋ฆฌ: map์„ ์‚ฌ์šฉํ•  ๋•Œ const char*๋ณด๋‹ค std::string์„ ์“ฐ๋Š” ๊ฒƒ์ด ์•ˆ์ „ํ•จ!