vs 2010のstdext::hash_map vs 2015での質問

1442 ワード

vs 2010のhash_map呼び出し方式:
ヘッダファイルとコマンド空間stdextは、異なるkeyタイプに対応するcomparatorを定義する必要があります.
#include <hash_map>
using namespace stdext;


struct intLess : public std::binary_function<const int, const int, bool>
{
public:
    result_type operator()(const first_argument_type& _Left, const second_argument_type& _Right) const
    {
        return _Left == _Right;
    }
};

struct charLess : public std::binary_function<const char*, const char*, bool>
{
public:
    result_type operator()(const first_argument_type& _Left, const second_argument_type& _Right) const
    {
        return(_stricmp(_Left, _Right) < 0 ? true : false);
    }
};

これで大変な問題が発生しましたmapのkeyはconstでなければなりません.この要求はvs 2012以前のバージョンでしかありません.gcc私が出会ったバージョンはありません.
 
vs 2015などの新しいバージョンを使用すると、このようなコンパイルエラーに遭遇します.
Microsoft Visual Studio 14.0\VC\INCLUDE\xstddef(377): error C2338: The C++ Standard doesn't provide a hash for this type.
 
その理由は、このconstが不要になったこと、またC++11がstd::unordered_を追加したことです.mapは、自分でcomparatorを実現する必要はなく、旧版のhash_よりも使います.mapの方が便利です.マイクロソフトが反人類のapiを設計しても穴があいている.
 
まとめ:vs 2010のstdext::hash_を除くmapのkeyはconstである必要がありますが、他の場合のmapはconstである必要はありません.std::unordered_mapのkeyはconstではなく、std::mapはconstでもconstでもなくてもよい.