#include <iostream>
#include <hash_map>
using namespace stdext;
using namespace std;
class CompareStdString
{
public:
bool operator()(const string & str1, const string & str2) const
{
return str1.compare(str2) < 0;
}
};
int main()
{
hash_map<string, int, hash_compare<string, CompareStdString> > Map;
Map.insert(make_pair("one", 1));
Map.insert(make_pair("two", 2));
Map.insert(make_pair("three", 3));
Map.insert(make_pair("four", 4));
Map.insert(make_pair("five", 5));
hash_map<string, int, hash_compare<string, CompareStdString> > :: iterator i;
for (i = Map.begin(); i != Map.end(); ++i)
{
i -> first; // they are ordered as three, five, two, four, one
}
return 0;
}
我想使用hash_map保持std :: string作爲一個鍵。但是,當我插入下一對訂單是困惑。爲什麼訂單不匹配插入訂單?我應該如何得到一個二三四五的訂單?stdext :: hash_map不清楚哈希函數
這引發了一個問題,爲什麼你不使用'std :: unordered_map' ... – PlasmaHH
我認爲'std :: map'會爲你想要的值排序而不需要額外的工作。 –
andre
@andre'std :: map'也不使用插入順序(至少不是默認情況下,並不容易獲得比較器)。 –