https://kimcoder.tistory.com/122
map<string, int> m;
m["hi"] -= 1;
cout << m["hi"];
map[key];
key 에 해당하는 value 값 반환
#include <iostream>
#include <map>
#include <string>
int main() {
map<int, string>::iterator ite; // 직접 이터레이터 선언도 가능
map<int, string> tempMap = {{1, "Apple",},
{2, "Banana",},
{3, "Mango",},
{4, "Raspberry",},
{5, "Blackberry",},
{6, "Cocoa",}};
for (auto iter = tempMap.begin(); iter != tempMap.end(); ++iter){
cout << "[" << iter->first << ","
<< iter->second << "]\n";
}
for (ite = tempMap.begin(); ite != tempMap.end(); ite++)
{
cout << "[" << ite->first << ","
<< ite->second << "]\n";
}
return 0;
}
// 두개의 결과는 같다
#include <string>
#include <vector>
#include <map>
#include <algorithm> // for sort
#include <iostream>
using namespace std;
bool compare(const pair<int, int> &first, const pair<int, int> &second) {
return first.second < second.second; // value 오름차순 정렬 위함
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
map<int, int> m;
m[1] = 2;
m[2] = 4;
m[3] = 3;
m[4] = 1;
vector<pair<int, int>> v(m.begin(), m.end()); // map -> vector
sort(v.begin(), v.end(), compare); // sort
for(int i = 0; i < v.size(); i++) {
cout << "key : " << v[i].first << ", value : " << v[i].second << "\n";
}
return 0;
}
// 출력
key : 4, value : 1
key : 1, value : 2
key : 3, value : 3
key : 2, value : 4
https://m.blog.naver.com/rapperkjm/221038507723
https://www.geeksforgeeks.org/map-vs-unordered_map-c/