この質問は、 char を stdmap のキーとして使用することに直接関係しています。
渡された比較関数の機能と、型にキーとして必要な理由を理解していchar *
ます。ただし、更新が実際にどのように機能するかはわかりません。
キーを更新している場合について知りたいです。std::map
間の同等性を比較する方法をどのように知っていますか。キーをツリーに挿入する順序をマップに伝えるだけですconst char *
。cmp_str
stl_tree.h
コードを少し掘り下げました( here から取得) が、あまり見つけることができませんでした。私の唯一の推測は、それが単純なメモリ比較を行っているということです。
stl_tree
下位クラスがこの状況をどのように処理するか、または常に正しく処理しない場合、どのエッジ ケースが壊れるかに興味がありますか?
コード
#include <map>
#include <iostream>
#include <cstring>
struct cmp_str
{
bool operator()(char const *a, char const *b)
{
return std::strcmp(a, b) < 0;
}
};
int main ( int argc, char ** argv )
{
std::map<const char*, int, cmp_str> map;
map["aa"] = 1;
map["ca"] = 2;
map["ea"] = 3;
map["ba"] = 4;
map["ba"] = 5;
map["bb"] = 6;
map["ba"] = 7;
std::map<const char*, int, cmp_str>::iterator it = map.begin();
for (; it != map.end(); it++ )
{
std::cout << (*it).first << ": " << (*it).second << std::endl;
}
return 0;
}
出力
aa: 1
ba: 7
bb: 6
ca: 2
ea: 3