1

My map is defined like this:

map<string, map<string, string>> _map; 

But the problem that has come up is that my map's first is mostly the same value, i.e.

_map["key1"]["value1"] = "data1"; _map["key1"]["value2"] = "data2"; _map["key1"]["value3"] = "data3"; _map["key1"]["value4"] = "data4"; _map["key2"]["value5"] = "data5"; 

So when I want to delete any particular (key, value), I can't use _map.find("key1") for the iterator because it has duplicate entries.

Is there a way to set the iterator index using the value?

Suppose I want to delete this entry:

_map["key1"]["value4"] = "data4"; 

How would I do this?

8
  • Your question isn't clear. It seems you're asking how to remove an element from a map, which is trivial to find in documentation. Could you clarify just what the problem you're trying to solve is? Commented Apr 11, 2016 at 7:01
  • You can't have more than one value per key in a map. Commented Apr 11, 2016 at 7:03
  • @A.Franzen: he doesn't have more than one value per key. look closely! Commented Apr 11, 2016 at 7:05
  • 3
    Oh I see, you call inner map "keys" "values". That is confusing. Commented Apr 11, 2016 at 7:10
  • 1
    There are no duplicated keys in your map. This, by definition, is impossible. Is like having duplicated indexes in a matrix: it is not possible. Commented Apr 11, 2016 at 7:14

2 Answers 2

4

Justerase it:

 _map["key1"].erase("value4"); 

Note that it doesn't throw exception if the key doesn't exist — it returns 0 in that case.

BTW, you can improve the initialization of the map as:

std::map<std::string, std::map<std::string, std::string>> _map { {"key1", { {"value1", "data1"}, {"value2", "data2"}, {"value3", "data3"}, {"value4", "data4"} } }, {"key2", { {"value5", "data5"} } } }; 
Sign up to request clarification or add additional context in comments.

Comments

3

Nawaz's answer is correct. It does have one weakness, however: If you erase "value5", _map["key2"] will still contain an empty map<string, string>. This is probably not what you intend.

To fix this, you can use the pair template and change the type of _map to be:

map<pair<string, string>, string> _map; 

Your assignments would then look like:

_map[make_pair("key1", "value1")] = "data1"; _map[make_pair("key1", "value2")] = "data2"; _map[make_pair("key1", "value3")] = "data3"; _map[make_pair("key1", "value4")] = "data4"; _map[make_pair("key2", "value5")] = "data5"; 

Then you can remove each entry like:

_map.erase(make_pair("key1", "value4")); 

You can find the pair type in the <utility> header.

8 Comments

Would his data still be organized in the same order initializing it your way instead of how he has it though? Asking because I genuinely don't know.
Yes, it would. std::pair defines a comparison, which orders first by the first coordinate, then by the second.
Erase gives me an error. It doesn't recognize the parameter. I'm not able to delete it like that. I tried passing a const pair also, still doesn't work.
error: no matching function for call to 'std::map<std::__cxx11::basic_string<char>, std::map<std::__cxx11::basic_string<char>, Session> >::erase(std::pair<std::__cxx11::basic_string<char>, Session>)' _map.erase(make_pair("contact", session));
Basically, it's not letting me pass a pair to erase.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.