2

次のような文字列でインデックス付けされた unordered_sets の順序付けられていないマップがある場合

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

このデータ構造の使用についていくつか質問がありました。セットへのポインターを使用したり、マップ値のインデックスを再作成したりすることなく、マップでインデックス付けされたセットに新しい値を挿入する方法はありますか?

2 番目の質問です。マップにインデックスを作成しようとすると、未解決の外部シンボル エラーが発生します。例として、

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 >,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

4

2 に答える 2

4

最初の質問、はい、結果を参照に割り当てる限り、問題はありません。

これを行う:

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.
于 2011-05-04T07:31:45.063 に答える
1

OK、未解決のシンボルは、静的変数をどこにもインスタンス化していないためです。C++でそれをしなければならなかったのを忘れていました、私の間違いです。セットのご協力ありがとうございます

于 2011-05-04T08:59:20.247 に答える