使用 unordered_sets 的 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:静态类 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个问题,是的,只要将结果分配给参考就应该可以。
执行此操作:
现在
set
是对地图中值的引用。 (我不确定auto
为您提供了什么,因此我将类型写完整;您也许可以不用使用auto
。)您所做的任何更改set
将反映在地图中。First question, yes, it should be fine as long as you assign the result to a reference.
Do this:
Now
set
is a reference to a value in the map. (I'm not sure whatauto
gives you, so I put the type in full; you may be able to get away with usingauto
.) Any changes you make toset
will be reflected in the map.好的,未解析的符号是因为我没有在任何地方实例化静态变量。我忘了你必须在 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