[TIL] c++ map Posted on 2022-06-30 | In TIL map에서 find 123456789101112131415161718192021222324252627#include <iostream> #include <map> using std::cout; using std::cin; using std::endl; using std::map; using std::string; int main(){ std::map<string, string> m1 = { {"h", "htop"}, {"k", "ktop"}, {"t", "ttop"}, {"r", "rtop"}, {"w", "wtop"}, {"p", "ptop"} }; string key = "h"; auto item = m1.find(key); //item은 주소값이 담긴다, item->first: 첫번째값(key 값) , item->second: 두번째값(value 값) if (item != m1.end()) { //m1.end()는 cout << "Key exists! - {" << item->first << ";" << item->second << "}\n"; } else { cout << "Key does not exist!" << endl; } return EXIT_SUCCESS; } map 순회 1234567891011121314151617181920212223242526272829303132333435363738394041/* map example */ #include <map> #include <string> #include <iostream> using std::map; using std::string; using std::cout; using std::endl; int main(void){ // <int, string> fair map test map<int, string> m; map<int, string>::iterator i; m.insert( map<int, string>::value_type(1, "Hello") ); m.insert( map<int, string>::value_type(2, "World") ); // map 순회 for(i = m.begin(); i != m.end(); i++){ cout << "[" << i->first << "] " << i->second << endl; } // direct select cout << m[1] << endl; // <string, string> fair map test map<string, string> m2; map<string, string>::iterator j; m2.insert( map<string, string>::value_type("a", "Hello") ); m2.insert( map<string, string>::value_type("123", "World") ); // map 순회 for(j = m2.begin(); j != m2.end(); j++){ cout << "[" << j->first << "] " << j->second << endl; } // direct select cout << m2["123"] << endl; }