2012-06-21 22 views
3

我一直在使multimap工作的问题。我将只显示代码和说明问题:multimap的错误(密钥类型是std :: string)

#include <string> 
    ... 

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

然而,当我编译,最后一行给了我以下错误:

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 ]

有谁知道的东西我可能是做错了?

+0

好吧,我已经尝试过(而且只是试图再次)与 '的std :: string MyString的= “测试”; arcList [了mystring] = PDE;' 和它给我同样的错误,所以,改变不修复它 –

+0

@Cameron R:然后相应地更新您的代码和编译错误。 –

回答

4

下面的代码是如何正确执行它的一个例子。

  1. 正如其他人所指出的,标准:: multimap中没有索引operator[],因为它没有任何意义提取出来的元素 - 有每个每个索引多个值。

  2. 您必须insert一个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)); 
} 
+0

非常感谢。是的,在阅读juanchopanza的评论后,我想到按索引的方式提取我想要的方式是没有意义的 –

6

这是因为std::multimap没有一个operator[]。尝试使用insert方法。

+0

啊,这是有道理的。我有类似的地图工作,这就是为什么这让我感到困惑。我想这是有道理的,它不会很容易索引一个multimap的关键。谢谢! –

相关问题