cpp_map - 8BitsCoding/RobotMentor GitHub Wiki
- ํค์ ๊ฐ์ ์๋ค์ ์ ์ฅ
- ํค๋ ์ค๋ณต๋ ์ ์์
- C++ ๋งต์ ์๋ ์ ๋ ฌ๋๋ ์ปจํ ์ด๋์ด๋ค. (์ฃผ์)
- ์ด์ง ํ์ ํธ๋ฆฌ ๊ธฐ๋ฐ์ด๊ธฐ์ ์ค๋ฆ์ฐจ์์ด๋ค. (ํค ๊ธฐ์ค)
- ๋งต ๋ง๋ค๊ธฐ
- ์ค๋ณต๋ ํค๋ฅผ ์ฝ์ ํ๋ค๋ฉด??
- ์๋์ ๋ ฌ
- ์์ ์ฐพ๊ธฐ
- swap, clear
- erase
- ๊ฐ์ฒด๋ฅผ ํค๋ก ์ฌ์ฉํ๊ธฐ
- ์ฅ/๋จ์
#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 : ๋ map์ ํค์ ๊ฐ์ ์๋ก ๋ง๋ฐ๊พผ๋ค.
- clear : map์ ๋น์ด๋ค.
std::map<std::string, int> scoreMap;
std::map<std::string, int> anotherScoreMap;
scoreMap.swap(anotherScoreMap);
anotherScoreMap.clear();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์ ํด๊ฒฐ์ฑ ์ด ์์