Map... please help

I have map<string,list<string> > wMap; and to insert new values I use wMap[word].push_back(file); which works but adds the same value twice even is its already in there. I am not sure how to access the values in the list individually. Several things that I would like to accomplish are testing to see if the value is already in the list, ie for the key a i can have values 1,2,3 but cannot have 1,2,2,3. If the value is already there dont add a second. Another is taking the values from the list and pushing them the the back of a vector. Please help I dont know where else to go.
Last edited on
maps can only have 1 entry for any unique key.

The reason you have multiple entries for the key is because you have a map of LISTS and not a map of strings.

IE:

1
2
3
4
5
6
map<string,list<string>> wMap;

wMap["a"].push_back("1");
wMap["b"].push_back("2");
wMap["b"].push_back("3");
wMap["c"].push_back("4");

The result is:

wMap has 3 entires:  one for "a", "b", and "c"

wMap["a"] is a list containing ["1"]
wMap["b"] is a list containing ["2", "3"]
wMap["c"] is a list containing ["4"]



If you want each entry in the map to only have one string and not a list of strings, then make your map a map of strings instead of a map of lists:

1
2
3
4
5
map<string,string> wMap;
wMap["a"] = "1";
wMap["b"] = "2";
wMap["b"] = "3";
wMap["c"] = "4";

The result is:

wMap still only has 3 entries:

wMap["a"] == "1"
wMap["b"] == "3"
wMap["c"] == "4"
Last edited on
Yea I understand and I need a key to be able to have more than one value that's why I chose a list. But is there no was to access that list within the map in-order to compare to see if the value is already there and other functions?
Sure there is....

wMap[key] gives you a reference to the list. You're already accessing it when you push_back.

1
2
3
list<string>& ref = wMap[key];

// here, ref is your list... so can do whatever you want with it 



Though if you want only unique elements in the list... consider using a set instead of a list. sets are the same idea with 3 differences:

1) faster searching for specific elements
2) DO NOT retain 'pushed in' order the same as lists do (ie: it's an ordered container, so values don't stay in the order you added them.. they get sorted)
3) Every entry is unique
Never Mind I over looked the comment in the code. I think that will do what I need it to do. I thank you very much for your assistance and time.
One more question if you would be so nice as to help me with. I have const functions that must be const. How can I use wMap.find(key) and still have a list ref list you listed above?
1
2
3
4
5
6
7
auto x = wMap.find(key);

if(x != wMap.end())
{
    // here x->second is your 'ref'
    x->second.size();  // etc
}



If auto doesn't work. Update your compiler (it's a C++11 thing, and pretty much every major compiler has supported it for years).

Or if that's not an option, replace it with this:

 
map<string,list<string>>::const_iterator x = wMap.find(key);
Last edited on
Topic archived. No new replies allowed.