3

I've been having problems with getting multimap to work. I'll 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 a conversion to a type acceptable to the predefined operator with [ _Kty=std::string, _Ty=Psdfwr::pinDelayElement ]

Does anyone know something I might be doing wrong?

2
  • ok, I've tried it before (and just tried it again) with std::string mystring = "test"; arcList[mystring] = pde; and it gives me the same error, so, changing that doesn't fix it Commented Jun 21, 2012 at 20:46
  • @Cameron R: Then update your code and compiler error accordingly. Commented Jun 21, 2012 at 20:48

2 Answers 2

6

That is because std::multimap doesn't have an operator[]. Try using the insert method.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, that makes sense. I had something similar working with map which is why this was confusing for me. I guess it makes sense that it wouldn't be so easy to index with just the key for a multimap. Thanks!
4

The code below is an example of how to do it properly.

  1. As others pointed out, std::multimap doesn't have the indexing operator[] because it makes no sense to extract elements out of it -- there are multiple values per each index.

  2. 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)); } 

1 Comment

Thanks a lot. Yeah, it occurred to me after reading juanchopanza's comment that it wouldn't make sense to extract by index the way I was trying to

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.