STL_map
【実行結果】
#include <iostream> #include <map> using namespace std; int main(){ map<string, int> testMap; testMap["test"] = 1234; if(testMap.find("test") != testMap.end()){ int i = testMap["test"]; cout << "test : " << i << "\n" << endl; } cout << "count : " << (unsigned int)testMap.size() << "\n" << endl; testMap.insert(map<string, int>::value_type("aaa", 1)); testMap.insert(map<string, int>::value_type("bbb", 22)); testMap.insert(map<string, int>::value_type("ccc", 333)); for(map<string, int>::iterator it = testMap.begin();it != testMap.end();++it){ string s = it->first; int i = it->second; cout << "string : " << s << endl; cout << "int : " << i << endl; } cout << "\ncount : " << (unsigned int)testMap.size() << "\n" << endl; cout << "クリアします" << endl; testMap.clear(); cout << "count : " << (unsigned int)testMap.size() << "\n" << endl; cout << "再度出力してみます" << "\n" << endl; for(map<string, int>::iterator it = testMap.begin();it != testMap.end();++it){ string s = it->first; int i = it->second; cout << "string : " << s << endl; cout << "int : " << i << endl; } return 0; }
【実行結果】
test : 1234 count : 1 string : aaa int : 1 string : bbb int : 22 string : ccc int : 333 string : test int : 1234 count : 4 クリアします count : 0 再度出力してみます