我正在尝试 std::map (在 MinGW 和 MSVC2008 中),我使用了以下代码:
#include <map>
#include <string.h>
#include <iostream>
using namespace std;
class MapManager
{public:
void insert(char* n){
cout << "Inserting " << n << endl;
m_map[n] = 0;}
void get(char* n){
cout << "Finding (" << n << ")" << endl;
int x = m_map[n];}
struct cmp_str{
bool operator()(char const *a, char const *b){
cout << "operator(" << a << " " << b << ")\n";
return strcmp(a, b) < 0;}
};
std::map<char*,int,cmp_str> m_map;
};
int main(){
MapManager m;
m.insert("Abc"); m.insert("Xyz"); m.insert("123"); m.insert("987");
m.get("Abc"); m.get("Xyz");
}
结果如下:
Inserting Abc
Inserting Xyz
operator(Abc Xyz)
operator(Xyz Abc)
operator(Abc Xyz)
operator(Xyz Abc)
Inserting 123
operator(Abc 123)
operator(123 Abc)
operator(123 Abc)
operator(Abc 123)
Inserting 987
operator(Abc 987)
operator(123 987)
operator(987 123)
operator(987 Abc)
operator(987 Abc)
operator(Abc 987)
operator(123 987)
operator(987 123)
Finding (Abc)
operator(Abc Abc)
operator(123 Abc)
operator(Abc 123)
operator(987 Abc)
operator(Abc 987)
operator(Abc Abc)
Finding (Xyz)
operator(Abc Xyz)
operator(Xyz Abc)
operator(Xyz Xyz)
operator(Xyz Xyz)
奇怪的是Inserting Xyz
需要 4 次调用才能进行比较!
operator(Abc Xyz)
operator(Xyz Abc)
operator(Abc Xyz)
operator(Xyz Abc)
map 用于插入/查找条目的算法是什么?