๐Ÿ“‚ Programming

[C++/STL] map ์ •๋ฆฌ

dhyuck 2021. 10. 4. 00:55
๋ฐ˜์‘ํ˜•

map์€ ํŠน์ • ์ˆœ์„œ์— ๋”ฐ๋ผ 'key-value'๋กœ ๋งคํ•‘๋œ ๊ฐ’์˜ ์กฐํ•ฉ์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

  • map ํ—ค๋”

      #include <map>
  • map ์„ ์–ธ

    map์€ ๊ธฐ๋ณธ์ ์œผ๋กœ key๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์ด๋œ๋‹ค.

      map<string, int> MP;
  • map ๋‚ด๋ฆผ์ฐจ์ˆœ ์„ ์–ธ

    ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ ์–ธํ•˜๋ฉด๋œ๋‹ค. grater<key ์ž๋ฃŒํ˜•> ์ถ”๊ฐ€

      map<string, int, greater<string>> MP;
  • map insert ์š”์†Œ ์‚ฝ์ž…

      MP["AAA"] = 4;  // ("AAA", 4) ์‚ฝ์ž…
      MP["BBB"] = 8;  // ("BBB", 8) ์‚ฝ์ž…
      MP.insert(make_pair("111", 9)); // ("111", 9) ์‚ฝ์ž…  
      MP.insert(make_pair("0", 100)); // ("0", 100) ์‚ฝ์ž…
  • map value ์ ‘๊ทผ

      // value ์ ‘๊ทผ
      cout << MP["111"] << "\n";  // 9 ์ถœ๋ ฅ
      cout << MP["AAA"] << "\n";  // 4 ์ถœ๋ ฅ
      cout << MP["YYY"] << "\n";  // 0 ์ถœ๋ ฅ
      cout << MP["ZZZ"] << "\n";  // 0 ์ถœ๋ ฅ
  • map value ์ˆ˜์ •

      // value ์ˆ˜์ •
      MP["AAA"] += 100;
      cout << MP["AAA"] << "\n";  // 104 ์ถœ๋ ฅ
  • map erase ์‚ญ์ œ

      // ์‚ญ์ œ
      MP.erase("AAA"); // "AAA"๋ฅผ ํ‚ค๋กœ ๊ฐ€์ง„ ์š”์†Œ ์‚ญ์ œ
      MP.erase("ZZZ"); // "ZZZ"๋ฅผ ํ‚ค๋กœ ๊ฐ€์ง„ ์—†๋Š” ์š”์†Œ ์‚ญ์ œ, ์—๋Ÿฌ ๋‚˜์ง€ ์•Š์Œ
  • map find ์ฐพ๊ธฐ

    ์ฐพ๋Š” key๊ฐ€ ์—†์œผ๋ฉด, end()๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

    ์•„๋ž˜๋Š” "999999 is not in MP"๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

      if(MP.find("999999") == MP.end())
          cout << "999999 is not in MP\n";
      else
          cout << "999999 is in MP\n";
  • map empty

    ๋น„์–ด์žˆ๋‹ค๋ฉด true, ๋น„์–ด์žˆ์ง€ ์•Š๋‹ค๋ฉด false๋ฅผ ๋ฐ˜ํ™˜

    ์•„๋ž˜๋Š” "MP is not empty" ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค

      if(MP.empty() == true)
          cout << "MP is empty\n";
      else
          cout << "MP is not empty\n";
  • map clear

      // clear
      MP.clear();
      cout << "MP size : " << MP.size() << "\n";  // MP size : 0
  • map ์ˆœํšŒ

      for(auto it : MP)
          cout << it.first << " " << it.second << "\n";

  • ์ „์ฒด ์†Œ์Šค์ฝ”๋“œ

      #include <iostream>
      #include <map>
      using namespace std;
      // Maps are associative containers that
      // store elements formed by a 'combination of a key value and a mapped value', following a specific order.
      int main()
      {
          // map ์„ ์–ธ
          // map์€ ๊ธฐ๋ณธ์ ์œผ๋กœ key๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์ด ๋œ๋‹ค.
          map<string, int> MP;
          // ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ ์–ธํ•˜๋ฉด๋œ๋‹ค. grater<key ์ž๋ฃŒํ˜•> ์ถ”๊ฐ€
          // map<string, int, greater<string>> MP;
    
          // ์š”์†Œ ์‚ฝ์ž…
          MP["AAA"] = 4;  // ("AAA", 4) ์‚ฝ์ž…
          MP["BBB"] = 8;  // ("BBB", 8) ์‚ฝ์ž…
          MP.insert(make_pair("111", 9)); // ("111", 9) ์‚ฝ์ž…  
          MP.insert(make_pair("0", 100)); // ("0", 100) ์‚ฝ์ž…
    
          // size
          cout << "MP size : " << MP.size() << "\n";  // MP size : 4
    
          // value ์ ‘๊ทผ
          cout << MP["111"] << "\n";  // 9 ์ถœ๋ ฅ
          cout << MP["AAA"] << "\n";  // 4 ์ถœ๋ ฅ
          cout << MP["YYY"] << "\n";  // 0 ์ถœ๋ ฅ
          cout << MP["ZZZ"] << "\n";  // 0 ์ถœ๋ ฅ
    
          // value ์ˆ˜์ •
          MP["AAA"] += 100;
          cout << MP["AAA"] << "\n";  // 104 ์ถœ๋ ฅ
    
          // ์‚ญ์ œ
          MP.erase("AAA"); // "AAA"๋ฅผ ํ‚ค๋กœ ๊ฐ€์ง„ ์š”์†Œ ์‚ญ์ œ
          MP.erase("ZZZ"); // "ZZZ"๋ฅผ ํ‚ค๋กœ ๊ฐ€์ง„ ์—†๋Š” ์š”์†Œ ์‚ญ์ œ, ์—๋Ÿฌ ๋‚˜์ง€ ์•Š์Œ
    
          // find
          // ์ฐพ๋Š” key๊ฐ€ ์—†์œผ๋ฉด, end()๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
          // ์•„๋ž˜๋Š” "999999 is not in MP"๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค
          if(MP.find("999999") == MP.end())
              cout << "999999 is not in MP\n";
          else
              cout << "999999 is in MP\n";
    
          // empty
          // ๋น„์–ด์žˆ๋‹ค๋ฉด true, ๋น„์–ด์žˆ์ง€ ์•Š๋‹ค๋ฉด false
          // ์•„๋ž˜๋Š” "MP is not empty" ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค
          if(MP.empty() == true)
              cout << "MP is empty\n";
          else
              cout << "MP is not empty\n";
    
          // MP ์ˆœํšŒ
          // ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅ๋œ๋‹ค.
          /*  0 100
              111 9
              BBB 8
              YYY 0   */
          for(auto it : MP)
              cout << it.first << " " << it.second << "\n";
    
          // clear
          MP.clear();
          cout << "MP size : " << MP.size() << "\n";  // MP size : 0
      }
๋ฐ˜์‘ํ˜•