cpp_map - 8BitsCoding/RobotMentor GitHub Wiki

์ •์˜

  • ํ‚ค์™€ ๊ฐ’์˜ ์Œ๋“ค์„ ์ €์žฅ
  • ํ‚ค๋Š” ์ค‘๋ณต๋  ์ˆ˜ ์—†์Œ
  • C++ ๋งต์€ ์ž๋™ ์ •๋ ฌ๋˜๋Š” ์ปจํ…Œ์ด๋„ˆ์ด๋‹ค. (์ฃผ์˜)
  • ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ ๊ธฐ๋ฐ˜์ด๊ธฐ์— ์˜ค๋ฆ„์ฐจ์ˆœ์ด๋‹ค. (ํ‚ค ๊ธฐ์ค€)

๋ชฉ์ฐจ


๋งต ๋งŒ๋“ค๊ธฐ

#include <map>

int main() {
    std::map<std::string, int> simpleScoreMap;

    simpleScoreMap.insert(std::pair<std::string, int>("Mocha", 100));
    simpleScoreMap.insert(std::pair<std::string, int>("CoCo", 50));

    simpleScoreMap["Mocha"] = 0;

    std::cout << "Current size: " << simpleScoreMap.size() << std::endl;
}

์ค‘๋ณต๋œ ํ‚ค๋ฅผ ์‚ฝ์ž… ํ•œ๋‹ค๋ฉด??

// ํ•จ์ˆ˜์˜ ์›ํ˜•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
std::pair<iterator, bool> insert(const value_type& value)
// <iterator, true> ๋ฐ˜ํ™˜
simpleScoreMap.insert(std::pair<std::string, int>("Mocha", 100));

// <iterator, false> ๋ฐ˜ํ™˜
simpleScoreMap.insert(std::pair<std::string, int>("Mocha", 50));

๋‹ค๋ฅด๊ฒŒ ๋„ฃ๋Š” ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์—†์„๊นŒ?

operator[]๋ฅผ ์ด์šฉ

  • key์— ๋Œ€์‘ํ•˜๋Š” ๊ฐ’์„ ์ฐธ์กฐ๋กœ ๋ฐ˜ํ™˜
  • map์— ํ‚ค๊ฐ€ ์—†์œผ๋ฉด ์ƒˆ ์š”์†Œ๋ฅผ ์‚ฝ์ž…
  • map์— ํ‚ค๊ฐ€ ์ด๋ฏธ ์žˆ์œผ๋ฉด ๊ทธ ๊ฐ’์„ ๋ฎ์–ด์”€
std::map<std::string, int> simpleScoreMap;

simpleScoreMap["CoCo"] = 10;        // ์ƒˆ ์š”์†Œ ์‚ฝ์ž…
simpleScoreMap["CoCo"] = 50;        // "CoCo" ๋ณ€๊ฒฝ

๋„ˆ๋ฌด ์ข‹์€๋ฐ??

์—ญ์‹œ ๋‹จ์ ์€ ์กด์žฌ

value = simpleScoreMap["CoCo"];
// ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ถˆ๋Ÿฌ์˜จ๋‹ค๋ฉด CoCo๋ž€ ๊ฐ’์ด ์žˆ์œผ๋ฉด ๋ถˆ๋Ÿฌ์™€ ์ง€์ง€๋งŒ ์—†์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์ด ์‚ฝ์ž…๋˜์–ด ๋ฆฌํ„ด๋œ๋‹ค.

์ž๋™์ •๋ ฌ

simpleScoreMap.insert(std::pair<std::string, int>("Mocha", 100));
simpleScoreMap.insert(std::pair<std::string, int>("CoCo", 50));

for(std::map<std::string, int>::iterator it = simpleSocreMap.begin(); it != simpleSocreMap.end(); ++it)
{
    std::cout << "(" << it->first << "," << it->second << ")" << std::endl;
}

CoCo, 50

Mocha, 100

์ˆœ์œผ๋กœ ์ €์žฅ๋จ


์š”์†Œ ์ฐพ๊ธฐ

#include <map>

int main() {
    std::map<std::string, int> simpleScoreMap;
    simpleScoreMap.insert(std::pair<std::string, int>("Mocha", 100));

    std::map<std::string, int>::iterator it = simpleScoreMap.find("Mocha");
    if(it!=simpleScoreMap.end())
    {
        it->second = 80;
    }
    return 0;
}

์ฐพ์€ ๊ฒฐ๊ณผ๋ฅผ ์ฐธ์กฐ๋กœ ๋ฐ˜ํ™˜ํ•จ์„ ๊ธฐ์–ตํ•˜์ž.


swap, clear

  • swap : ๋‘ map์˜ ํ‚ค์™€ ๊ฐ’์„ ์„œ๋กœ ๋งž๋ฐ”๊พผ๋‹ค.
  • clear : map์„ ๋น„์šด๋‹ค.
std::map<std::string, int> scoreMap;
std::map<std::string, int> anotherScoreMap;

scoreMap.swap(anotherScoreMap);

anotherScoreMap.clear();

erase

std::map<std::string, int>::iterator foundIt = SimpleScoreMap.find("CoCo");
SimpleScoreMap.erase(foundIt);

SimpleScoreMap.erase("CoCo");

๊ฐœ์ฒด๋ฅผ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜๊ธฐ

class StudentInfo
{
public:
    // ์ƒ์„ฑ์ž
private:
    std::string mName;
    std::string mStuentID;
}
int main() {
    std::map<StudentInfo, int> Scores;

    Scores.insert(std::pair<StudentInfo, int>(StudentInfo("Poppoy", "a556"), 30));
}

๋นŒ๋“œ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ

Key๊ฐ€ ์ •๋ ฌ๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Operator<()์˜ ์ •์˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

bool StudentInfo::opertor<(const StudentInfo& other) const
{
    if(mName == other.mName)
    {
        // ์ด๋ฆ„์ด ๊ฐ™์€๊ฒฝ์šฐ๋Š” ํ•™๋ฒˆ์œผ๋กœ ์ •๋ ฌ
        return mStudentID < other.mStudentID;
    }

    // ๋””ํดํŠธ๋Š” ์ด๋ฆ„์œผ๋กœ ์ •๋ ฌ
    return mName < other.mName;
}

์œ„ ๋ฐฉ๋ฒ•์„ ๋ชป ์“ธ๋•Œ๋ฉด? (์˜ˆ๋ฅผ๋“ค์–ด ๋‚ด๊ฐ€๋งŒ๋“  ์ฝ”๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ๋˜์ง€?)

struct StudentInfoComaparer
{
    bool operator()(const StudentInfo& left, const StudentInfo& right) const
    {
        return (left.getName() < right.getName());
    }
};

std::map<StudentInfo, int, StudentInfoComparer> Scores;

์žฅ/๋‹จ์ 

์žฅ์ 

  • std::list๋‚˜ std::vector๋ณด๋‹ค ํƒ์ƒ‰ ์†๋„๊ฐ€ ๋” ๋น ๋ฆ„

๋‹จ์ 

  • ์ž๋™์œผ๋กœ ์ •๋ ฌ๋จ

  • ํ•ด์‰ฌ๋งต์ด ์•„๋‹ˆ๊ธฐ์— ๊ฒ€์ƒ‰์†๋„๊ฐ€ O(1)

  • C++11์— ํ•ด๊ฒฐ์ฑ…์ด ์žˆ์Œ

โš ๏ธ **GitHub.com Fallback** โš ๏ธ