STL无序地图 - 插入矢量

时间:2015-03-28 02:22:52

标签: c++ vector stl insert unordered-map

我对STL的无序地图不太熟悉,我在插入元素时遇到了麻烦。

所以我有一个无序的映射,其中键是一个字符串,映射的值是整数的向量。我把它声明为:

              unordered_map<string, vector<int> > categorySearch;

我如何在此地图中插入元素。我正在做

       categorySearch.insert(make_pair("hello", categorySearch["hello"].push_back(5)));

这显然给了我编译器错误。如何使用insert作为地图的矢量部分。

这是我得到的编译器错误:

logData.h: In member function ‘void LogData::addEntry(Log)’:
logData.h:23:68: error: no match for ‘operator[]’ (operand types are ‘std::unordered_map<std::basic_string<char>, std::vector<int> >’ and ‘<unresolved overloaded function type>’)
categorySearch.insert(make_pair(add.getCategory(), categorySearch[add.getCategory].push_back(data.size()-1)));
                                                                ^
logData.h:23:68: note: candidates are:
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/unordered_map:48:0,
             from logData.h:2,
             from logman.cpp:5:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/unordered_map.h:595:7: note: std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type& std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator[](const key_type&) [with _Key = std::basic_string<char>; _Tp = std::vector<int>; _Hash = std::hash<std::basic_string<char> >; _Pred = std::equal_to<std::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::basic_string<char>, std::vector<int> > >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type = std::vector<int>; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_type = std::basic_string<char>]
   operator[](const key_type& __k)
   ^
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/unordered_map.h:595:7: note:   no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘const key_type& {aka const std::basic_string<char>&}’
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/unordered_map.h:599:7: note: std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type& std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator[](std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_type&&) [with _Key = std::basic_string<char>; _Tp = std::vector<int>; _Hash = std::hash<std::basic_string<char> >; _Pred = std::equal_to<std::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::basic_string<char>, std::vector<int> > >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type = std::vector<int>; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_type = std::basic_string<char>]
   operator[](key_type&& __k)
   ^
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/unordered_map.h:599:7: note:   no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::unordered_map<std::basic_string<char>, std::vector<int> >::key_type&& {aka std::basic_string<char>&&}’
make: *** [logman.o] Error 1

感谢。

2 个答案:

答案 0 :(得分:2)

我想我采取两种方法之一。第一种可能性是使用地图operator[],因此代码如下:

category_search["hello"].push_back(5);

另一个显而易见的可能性是识别您正在创建的内容(每个键具有多个映射值)等同于unordered_multimap提供的内容,因此您可以使用:

std::unordered_multimap<std::string, int> category_search;

category_search.emplace("hello", 5);

请注意,我还使用emplace来简化代码(作为奖励,也可能运行得更快一些)。

是的,这两个都支持多个值,因此您可以添加:

category_search["hello"].push_back(10);

......或:

category_search.emplace("hello", 10);

...分别到unordered_map / unordered_multimap也将值10与键hello相关联。

如果是unordered_multimap,您可以使用equal_range检索与特定密钥相关联的值集,例如:

auto p = category_search.equal_range("hello");

这会将范围的开头设为p.first,将结尾设为p.second

答案 1 :(得分:1)

对中的secondvector<int>,因此您构建了一个向量,其中1个元素初始化为5而不是push_back

 categorySearch.insert(make_pair("hello", std::vector<int>(1,5)));

实例:http://ideone.com/JlMUuN

如果该项已经存在,请使用unordered_map::insert给出的返回值,该值是由迭代器组成的std::pair和表示插入成功的bool

#include <vector>
#include <unordered_map>
#include <string>
#include <iostream>

using namespace std;
int main()
{
    typedef unordered_map<string, vector<int>> CMap;
    CMap category_map;
    category_map.insert(make_pair("hello", std::vector<int>(1, 5)));
    auto pr = category_map.insert(make_pair("hello", std::vector<int>(1, 5)));
    if (!pr.second)
        pr.first->second.push_back(10);
    cout << pr.first->second.size(); 
}

直播示例:http://ideone.com/svEqcD

您将看到“hello”的地图条目现在在向量中有2个项目,因为由于先前存在的项目,对insert的第二次调用失败。

见这里:http://en.cppreference.com/w/cpp/container/unordered_map/insert

相关问题