使用unordered_sets的unordered_map

时间:2011-05-04 05:18:34

标签: c++ boost unordered-map

如果我有一个由字符串索引的unordered_sets的无序映射,例如

static boost::unordered_map<std::string, boost::unordered_set<std::string> > UseMap;

我有一些关于使用这种数据结构的问题。反正我是否在地图中索引的集合中插入新值而不必使用指向集合的指针或重新索引地图值?

第二个问题,当我尝试索引到地图时,我得到一个未解决的外部符号错误。例如,

void AddUse(const std::string &character, const std::string& used)
{
    auto set = UseMap[character];
    set.insert(used);
    UseMap[character] = set;

}

我不确定为什么会导致无法解决的符号错误,因此任何指导都会有所帮助。

提前致谢

编辑:任何UseMap [character]的使用都会导致未解决的符号错误

还添加了错误代码和源代码示例

全班

#pragma once
#ifndef _SINGLEUSE_H_
#define _SINGLEUSE_H_
#include <boost/unordered_map.hpp>
#include <boost/unordered_set.hpp>
#include <string>
#include <vector>
class SingleUse
{
public:
    void AddUse(const std::string& character, const std::string& used)
    {
        UseMap[character].insert(used);
    }

    bool HasUsed(const std::string &character, const std::string& used)
    {
        return false;//UseMap[character].find(used) != UseMap[character].end();
    }

    void ClearAll()
    {
        UseMap.clear();
    }
private:
    static boost::unordered_map<std::string, boost::unordered_set<std::string> > UseMap;
};

完整的错误消息:

错误52错误LNK2001:未解析的外部符号“private:static class boost :: unordered_map,class std :: allocator&gt ;, class boost :: unordered_set,class std :: allocator&gt ;, struct boost :: hash,class std :: allocator&gt;&gt;,struct std :: equal_to,class std :: allocator&gt;&gt;,class std :: allocator,class std :: allocator&gt;&gt;&gt;,struct boost :: hash,class std :: allocator&gt;&gt ;, struct std :: equal_to,class std :: allocator&gt;&gt;,class std :: allocator,class std :: allocator&gt; const,class boost :: unordered_set,class std :: allocator&gt ;, struct boost :: hash,class std :: allocator&gt;&gt ;, struct std :: equal_to,class std :: allocator&gt;&gt;,class std :: allocator,class std :: allocator&gt;&gt; ;&gt;&gt;&gt;&gt; SingleUse :: UseMap“(?UseMap @ SingleUse @@ 0V?$ unordered_map @ V?$ basic_string @ DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ 2 @ @ @@ STD V'$ unordered_set @ V'$ basic_string的@ DU?$ char_traits @ d @ @@ STD V'$分配器@ d @ @@ 2 STD @@ U&$散列@ V'$ basic_string的@ DU?$ char_traits @ d @性病@@ V'$ @分配器@ d @@ 2 STD @@@提振@@ú? $ equal_to @ V'$ basic_string的@ DU?$ char_traits @ d @ @@ STD V'$分配器@ d @ @@ 2 STD @@@ 2 @ V'$分配器@ V'$ basic_string的@ DU?$ char_traits @ d @性病@@ V'$ @分配器@ d @@ 2 STD @@@ 2 @@提高@@ U&$ @哈希V'$ @的basic_string杜?$ @ char_traits @ d @@性病V'$ @分配器d @ @@ 2 STD @@@ 4 @ U&$ equal_to @ V'$ basic_string的@ DU?$ char_traits @ d @ @@ STD V'$分配器@ d @ @@ 2 STD @@@ 2 @ V'$分配器@U?$ @对CBV $$?$ basic_string的@ DU?$ char_traits @ d @ @@ STD V'$分配器@ d @ @@ 2 STD @@ V'$ unordered_set @ V'$ basic_string的@ DU?$ char_traits @ d @ @@ STD V'$分配器@ d @ @@ 2 STD @@ U&$散列@ V'$ basic_string的@ DU?$ char_traits @ d @ @@ STD V'$分配器@ d @ @@ 2 STD @@@提振@@ U&$ @ equal_to V'$ @的basic_string杜?$ @ char_traits @ d @@性病V'$ @分配器@ d @@ 2 STD @@@ 2 @ V'$ @分配器V'$ basic_string @ DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ 2 @@ std @@@ 2 @@ boost @@@ std @@@ 2 @@ boost @@ A)G:\ Documents \编程项目\ KHMP \ KHMP_Repo \ KHMP \ build \ KHMP \ KHMP \ KHMPMain.obj

2 个答案:

答案 0 :(得分:3)

第一个问题,是的,只要您将结果分配给参考,就应该没问题。

这样做:

boost::unordered_set<std::string>& set = UseMap[character];

现在set是对地图中值的引用。 (我不确定auto给你的是什么,所以我把这个类型填写完整;你可以使用auto来逃避。)你对set所做的任何更改都将反映在地图上。

set.insert(used); // This updates the map, no need to write it back in.

答案 1 :(得分:1)

好的,未解析的符号是因为我没有在任何地方实例化静态变量。我忘了你必须在C ++中这样做,我的错误。感谢您对套装的帮助