STLのmapはいわゆる連想配列です
中の構造は2分木構造をしています
文字列と数字を関連つけたり
数字と数字を関連つけてデータにできます
main.cpp
autoはC++11からなので
コンパイルオプションに11以上のバージョン
中の構造は2分木構造をしています
map<firstの型,secondの型> 変数
文字列と数字を関連つけたり
数字と数字を関連つけてデータにできます
main.cpp
#include <iostream> #include <map> #include <string> int main() { std::map<std::string, int> m1; m1["language"] = 2001; m1["C++"] = 1990; m1["pascal"] = 1975; std::cout << m1["pascal"] << std::endl; std::map<int, int> m2; m2[1] = 100; m2[2] = 200; std::cout << m2[2] << std::endl; for(auto itr = m1.begin(); itr != m1.end(); itr++) { std::cout << itr->first << " , " << itr->second << std::endl; } }
autoはC++11からなので
コンパイルオプションに11以上のバージョン
-std=c++11をつける必要があります
コメントをかく