質問

C ++ STLを試してみるのは初めてです。 MAPを使用して多次元連想配列を構築しようとしています。例えば:

typedef struct DA {
    string  read_mode;
    string  data_type;
    void    *pValue;
    void    *pVarMemLoc;
}DA;

int main()
{
    map<string, map<string, map<string, map<string, map<string, DA*>>>>> DATA;

    DATA["lvl1"]["stg1"]["flr1"]["dep1"]["rom1"] = new DA;
    DATA["lvl1"]["stg1"]["flr1"]["dep1"]["rom2"] = new DA;
    DATA["lvl1"]["stg1"]["flr1"]["dep1"]["rom3"] = new DA;

    IEC["lvl1"]["stg1"]["flr1"]["dep1"]["rom1"]->read_mode = "file";
    IEC["lvl1"]["stg1"]["flr1"]["dep1"]["rom2"]->read_mode = "poll";
    IEC["lvl1"]["stg1"]["flr1"]["dep1"]["rom3"]->read_mode = "report";

    return 0;
}

VS2005で上記のコードをコンパイルすると、C4503の警告が170件ありました。すべての警告は、「装飾された名前の長さを超え、名前は切り捨てられた」というものです。しかし、プログラムは正常に実行されているようです。

これらの警告を引き起こした理由を私に説明するために、ある程度の時間を節約することを気にします。どうすればemを解決できますか?前もって感謝します :)

Warning 1   warning C4503: 'std::map<_Kty,_Ty>::~map' : decorated name length exceeded, name was truncated  c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 2   warning C4503: 'std::map<_Kty,_Ty>::map' : decorated name length exceeded, name was truncated   c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 3   warning C4503: 'std::map<_Kty,_Ty>::operator []' : decorated name length exceeded, name was truncated   c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 4   warning C4503: 'std::_Tree<_Traits>::~_Tree' : decorated name length exceeded, name was truncated   c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 5   warning C4503: 'std::map<_Kty,_Ty>::operator []' : decorated name length exceeded, name was truncated   c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 6   warning C4503: 'std::_Tree<_Traits>::iterator::~iterator' : decorated name length exceeded, name was truncated  c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 7   warning C4503: 'std::_Tree<_Traits>::iterator::iterator' : decorated name length exceeded, name was truncated   c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
役に立ちましたか?

解決

データ構造のこのモンスターを維持するつもりなら、それを無効にする以外の警告についてできることはほとんどありません。

#pragma warning(disable:4503)

他のヒント

私は警告を無効にするのはファンではありません。なぜなら、私の研究によれば、この警告に起因する意図しない結果が生じる可能性があるため、問題を本当に省くことを好むからです。

コードの書き換え方法は次のとおりです。

typedef struct DA {
    string  read_mode;
    string  data_type;
    void    *pValue;
    void    *pVarMemLoc;
}DA;
struct ROOM{
    map<string, DA*> map;
};
struct DEPARTMENT{
    map<string, ROOM> map;
};
struct FLOOR{
    map<string, DEPARTMENT> map;
};
struct STAGE{
    map<string, FLOOR> map;
};   
struct LEVEL{
    map<string, STAGE> map;
};   

そして、あなたはこのようにそれを使用することができます:

int main()
{
    LEVEL DATA;

    DATA.map["lvl1"].map["stg1"].map["flr1"].map["dep1"].map["rom1"] = new DA;
    DATA.map["lvl1"].map["stg1"].map["flr1"].map["dep1"].map["rom2"] = new DA;
    DATA.map["lvl1"].map["stg1"].map["flr1"].map["dep1"].map["rom3"] = new DA;

    ...
    etc

私の懸念と究極のソリューションは主に派生しています MSDN.

他の人は、警告を無効にする方法を提案しました。代わりにデザインを再考することをお勧めします。 Map^5よりも多くの抽象化を使用します。または、ストレージのデータ構造を変更します。たとえば、マップ^5の代わりにマップを使用します。

更新しました:

つまり、基本的に2つのオプションがあるということです。

  • 必要なだけ多くの文字列/レベルを備えたキーを使用します。

    struct Key3 { std::string x, y, z; }; typedef std::map<Key3, DA*> MyMap;

  • または、各レベルがda*値および/または別のレベルのいずれかを保持できる一般的なものを構築します。

そのような方法で宣言する(完成した引用に注意を払う)

map<string, map<string, map<string, map<string, map<string, DA*> > > > > DATA;

C ++は認識します >> シフト演算子として。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top