• Source
    1. #include <iostream>
    2. #include <unordered_map>
    3. using namespace std;
    4.  
    5. int main() {
    6. unordered_map<string, int> strs {
    7. { "one", 1 },
    8. { "two", 2 },
    9. { "three", 3 }
    10. };
    11.  
    12. // 添え字でアクセス可能
    13. cout << strs["one"] << endl;
    14.  
    15. cout << "-----" << endl;
    16.  
    17. // Iteratorで使う場合は fist, second で添え字と要素にアクセスする
    18. for (unordered_map<string, int>::iterator it = strs.begin(); it != strs.end(); it++) {
    19. cout << it->first << ":" << it->second << endl;
    20. }
    21.  
    22. cout << "-----" << endl;
    23.  
    24. // 要素の追加もラク
    25. strs["for"] = 4;
    26.  
    27. for (unordered_map<string, int>::iterator it = strs.begin(); it != strs.end(); it++) {
    28. cout << it->first << ":" << it->second << endl;
    29. }
    30.  
    31. return 0;
    32. }