Errors with multimap (key type - std :: string)

I'm having problems with the multimap. I will just show the code and describe the problem:

#include <string> ... multimap<std::string, pinDelayElement> arcList pinDelayElement pde; std:string mystring = "test" arcList[mystring] = pde; 

However, when I compile, the last line gives me the following error:

error C2676: binary '[': 'std :: multimap <_Kty, _Ty>' does not define this operator or conversion to a type acceptable for a predefined operator with [_Kty = std :: string, _Ty = Psdfwr :: pinDelayElement]

Does anyone know something that I can do wrong?

+3
source share
2 answers

The following is an example of how to do this.

  • As others have pointed out, std :: multimap does not have operator[] indexing, because it makes no sense to extract elements from it - there are several values ​​in each index.

  • You must insert a multimap<...>::value_type .

 #include <string> #include <map> void test() { typedef std::multimap<std::string, int> Map; Map map; map.insert(Map::value_type("test", 1)); } 
+4
source

This is because std :: multimap does not have operator[] . Try using the insert method.

+6
source

Source: https://habr.com/ru/post/1210650/


All Articles