errors with multimap (key type is std::string)

给你一囗甜甜゛ 提交于 2020-01-04 04:34:33

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/11146442/errors-with-multimap-key-type-is-stdstring

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!