lundi 29 juin 2015

inserting elements with duplicate keys gives same result std::map and std::multimap


I am writing a program that implements a uni-directional graph using std::map or std::multimap. I have to search for the key and then copy the vector values corresponding to that key into a new location in the map. Specifically, I am copying elements corresponding to 'find_key' into location pointed by 'mykey'. I find that both map and multimap give me the same result (no duplicate keys). How can I fix this using multimap? mymap is a map, mymultimap is a multimap. mymap[find_key] contains the vector values that I am trying to copy. I expected to be able to copy them (while retaining the original values in the multimap) using multimap. But it does not work. This is what I have so far:

for (vector<string>::const_iterator itr =mymap.find(mykey)->second.begin();    itr!=mymap.find(mykey)->second.end();++itr){

string find_key;
stringstream sso(*itr);
if (! (sso >> find_key))
{
cout<< "ERROR failed to convert value"<<endl;
}

 auto& mapit= mymap.find(find_key); 

   if (mapit == mymap.end()) {
    std::cout << "key not found" << std::endl;
    } else {
    mymap.insert(pair<string, vector<string>>(mykey,mymap[find_key]));
    mymultimap.insert(pair<string, vector<string>>(mykey,mymap  [find_key]));
}}}


Aucun commentaire:

Enregistrer un commentaire