Using an unordered_map of unordered_sets

Using an unordered_map of unordered_sets

本文关键字:unordered sets of map Using an      更新时间:2023-10-16

如果我有一个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;
}

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

Thanks in advance

编辑:任何使用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::分配器>,boost:: unordered_set类std::类分配器>,boost::哈希,struct类std::分配器>>,struct std:: equal_to类std::分配器>>,类std::分配器,类std::分配器>>>,boost::哈希,struct类std::分配器>>,struct std:: equal_to类std::分配器>>,类std::分配器,类std::分配器> const, boost:: unordered_set类std::类分配器>,boost::哈希,struct类std::分配器>>,结构体std::allocator>>, std::allocator, 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美元? 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美元?美元的方程l_to@V basic_string@DU美元? char_traits@D@std@@V ?美元allocator@D@2@@std@@@2@V ? allocator@U美元?美元pair@ cb 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@@@std@@@2@@boost@@A美元)旅客: 编程项目文档 KHMP KHMP_Repo KHMP 制造 KHMP KHMP KHMPMain.obj

第一个问题,是的,只要你把结果赋值给引用就应该没问题。

这样做:

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.

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