two keys map in C++ -
i plan use map 2 keys assignment. , create map following:
map<pair<string, string>, int> mymap; map<pair<string, string>, int>:: iterator it;
i had hard time on how use map.find() , map.insert() finding existing entry in map or insert new value if 2 keys combination new. can 1 give example?
it should same map, except have make pairs key.
insert :
map< pair<string,string>, int > mymap; pair<string, string> key = make_pair("bob", "sue"); mymap[ key ] = 5; // can inline make_pair if prefer. // or can use insert method mymap.insert( key, 5 );
find :
pair<string, string> key = make_pair("bob", "sue"); auto = mymap.find( key ); // can inline make_pair if prefer. if ( != mymap.end() ) { cout << it->second; }
note using strings key in map can have performance issues. also, order of strings in pair has significance.
Comments
Post a Comment