使用 unordered_sets 的 unordered_map

发布于 2024-11-05 09:15:34 字数 2945 浏览 0 评论 0原文

如果我有一个由字符串索引的 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:静态类 boost::unordered_map,类 std::allocator >,类 boost::unordered_set,类 std::allocator >,结构 boost::hash,类 std::allocator > >,结构 std::equal_to,类 std::allocator > ; >,类 std::分配器 > >,结构 boost::hash,类 std::分配器 > >,结构 std::equal_to,类 std::分配器 > ; >,类 std::allocator,类 std::allocator > const,类 boost::unordered_set,类 std::allocator >,struct boost::hash,类 std::allocator > std::equal_to,类 std::allocator > >,类 std::allocator > > > > 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?$hash@V?$basic_string@DU?$char_traits@D@std@@V?$分配器@D@2@@std@@@ boost@@U?$equal_to@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@V?$basic_string@DU ?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@boost@@U?$hash@V?$basic_string@DU?$char_traits@D@std@@V ?$allocator@D@2@@std@@@4@U?$equal_to@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2 @V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$unordered_set@V?$basic_string @DU?$char_traits@D@std@@V?$allocator@D@2@@std@@U?$hash@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D @2@@std@@@boost@@U?$equal_to@V?$basic_string@DU?$char_traits@D@std@@V?$分配器@D@2@@std@@@2@V?$分配器@V?$basic_string@DU?$char_traits@D@std@@V?$分配器@D@2@@std@@@2@@boost@@@std@@@2@@boost@@A) G:\Documents\Programming Projects\KHMP\KHMP_Repo\KHMP\build\KHMP\KHMP\KHMPMain.obj

If I have an unordered map of unordered_sets indexed by strings, such as

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

I had a couple question about using this data structure. Is there anyway for me to insert a new value into the set indexed in the map without having to use a pointer to the set or reindex the map value?

Second question, I'm getting an unresolved external symbol error when I try to index into the map. As an example,

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

}

I'm not sure why this is causing an unresolved symbol error, so any guidance there would be helpful.

Thanks in advance

EDIT: Any use of UseMap[character] causes the unresolved symbol error

Also added error code and source example

Full Class

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

And the full error message:

Error 52 error LNK2001: unresolved external symbol "private: static class boost::unordered_map,class std::allocator >,class boost::unordered_set,class std::allocator >,struct boost::hash,class std::allocator > >,struct std::equal_to,class std::allocator > >,class std::allocator,class std::allocator > > >,struct boost::hash,class std::allocator > >,struct std::equal_to,class std::allocator > >,class std::allocator,class std::allocator > const ,class boost::unordered_set,class std::allocator >,struct boost::hash,class std::allocator > >,struct std::equal_to,class std::allocator > >,class std::allocator,class std::allocator > > > > > > 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?$allocator@D@2@@std@@U?$hash@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@boost@@U?$equal_to@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@boost@@U?$hash@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@4@U?$equal_to@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$unordered_set@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@U?$hash@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@boost@@U?$equal_to@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@boost@@@std@@@2@@boost@@A) G:\Documents\Programming Projects\KHMP\KHMP_Repo\KHMP\build\KHMP\KHMP\KHMPMain.obj

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吝吻 2024-11-12 09:15:34

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

执行此操作:

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.

First question, yes, it should be fine as long as you assign the result to a reference.

Do this:

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

Now set is a reference to a value in the map. (I'm not sure what auto gives you, so I put the type in full; you may be able to get away with using auto.) Any changes you make to set will be reflected in the map.

set.insert(used); // This updates the map, no need to write it back in.
初相遇 2024-11-12 09:15:34

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

Ok, the unresolved symbol is because I don't instantiate the static variable anywhere. I forgot you had to do that in C++, my mistake. Thanks for the help with the sets

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文