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.
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:
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?
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
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?