C++ map
map을 사용하기 위해서는 <map 헤더를 인클루드 해야한다.
key와 value를 쌍으로 저장한다.
set과 마찬가지로 key를 중복으로 저장할 수 없으며, 중복된 key를 이용하려면 multimap을 이용하면 된다.
map은 []연산자를 이용하여 key에 접근할 수 있다.
map은 pair객체로 원소를 저장하기 때문에, 첫번째 key는 first로 두번째 value는 second로 접근할 수 있다.
생성자 사용 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <iostream> #include <map> #include <functional> int main() { std::map<char, int> first; first['a'] = 10; first['b'] = 30; first['c'] = 50; first['d'] = 70; std::map<char, int> second(first.begin(), first.end()); std::map<char, int> third(second); std::map<char, int, std::greater<int> > fourth = { {'a', 10}, {'b', 30}, {'c', 50}, {'d', 70} }; std::cout << "first : "; for (auto it = first.begin(); it != first.end(); it++) std::cout << (*it).first << ' ' << (*it).second << ' '; std::cout << '\n'; std::cout << "second : "; for (auto it = second.begin(); it != second.end(); it++) std::cout << (*it).first << ' ' << (*it).second << ' '; std::cout << '\n'; std::cout << "third : "; for (auto it = third.begin(); it != third.end(); it++) std::cout << (*it).first << ' ' << (*it).second << ' '; std::cout << '\n'; std::cout << "fourth : "; for (auto it = fourth.begin(); it != fourth.end(); it++) std::cout << (*it).first << ' ' << (*it).second << ' '; std::cout << '\n'; return 0; } | cs |
실행 결과
멤버함수 사용 예제
insert()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include <iostream> #include <map> int main() { std::map<char, int> mymap; // first insert function version (single parameter): mymap.insert(std::pair<char, int>('a', 100)); mymap.insert(std::pair<char, int>('z', 200)); std::pair<std::map<char, int>::iterator, bool> ret; ret = mymap.insert(std::pair<char, int>('z', 500)); if (ret.second == false) { std::cout << "element 'z' already existed"; std::cout << " with a value of " << ret.first->second << '\n'; } // second insert function version (with hint position): std::map<char, int>::iterator it = mymap.begin(); mymap.insert(it, std::pair<char, int>('b', 300)); // max efficiency inserting mymap.insert(it, std::pair<char, int>('c', 400)); // no max efficiency inserting // third insert function version (range insertion): std::map<char, int> anothermap; anothermap.insert(mymap.begin(), mymap.find('c')); // showing contents: std::cout << "mymap contains:\n"; for (it = mymap.begin(); it != mymap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; std::cout << "anothermap contains:\n"; for (it = anothermap.begin(); it != anothermap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; return 0; } | cs |
find(), erase()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include <iostream> #include <map> int main () { std::map<char,int> mymap; std::map<char,int>::iterator it; // insert some values: mymap['a']=10; mymap['b']=20; mymap['c']=30; mymap['d']=40; mymap['e']=50; mymap['f']=60; it=mymap.find('b'); mymap.erase (it); // erasing by iterator mymap.erase ('c'); // erasing by key it=mymap.find ('e'); mymap.erase ( it, mymap.end() ); // erasing by range // show content: for (it=mymap.begin(); it!=mymap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; return 0; } | cs |
count()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> #include <map> int main () { std::map<char,int> mymap; char c; mymap ['a']=101; mymap ['c']=202; mymap ['f']=303; for (c='a'; c<'h'; c++) { std::cout << c; if (mymap.count(c)>0) std::cout << " is an element of mymap.\n"; else std::cout << " is not an element of mymap.\n"; } return 0; } | cs |
참조: http://www.cplusplus.com/reference/map/map/
참고문헌: 뇌를 자극하는 C++ STL
'컴퓨터공학 > STL' 카테고리의 다른 글
[STL] C++ STL algorithm1 (0) | 2018.07.30 |
---|---|
[STL] C++ STL multimap (0) | 2018.07.22 |
[STL] C++ STL multiset (0) | 2018.07.22 |
[STL] C++ STL set (0) | 2018.07.22 |
[STL] C++ STL string to int/float/double/long long (0) | 2018.07.19 |