私は初心者であり、ハッシュテーブルに問題があります。私のプログラムにはハッシュテーブル構造が必要です。まず、boost unordered_mapを使用します。私に必要なすべてのものがありますが、私のプログラムはとても遅くなります。 stl hash_mapをテストしたいのですが、私が必要とするすべてのことを行うことはできません。これは私の最初のコード(これはサンプルです)Stlでキーを検索するHash_map
#include <hash_map>
using namespace std;
struct eqstr
{
bool operator()(int s1, int s2) const
{
return s1==s2;
}
};
typedef stdext::hash_map< int, int, stdext::hash_compare< int, eqstr > > HashTable;
int main()
{
HashTable a;
a.insert(std::pair<int,int>(1, 1));
a.insert(std::pair<int,int>(2, 2));
a.insert(std::pair<int,int>(4, 4));
//next i want to change value of key 2 to 20
a[2] = 20;
//this code only insert pair<2,20> into a, buy when I use boost unordered_map this code modify previous key of 2
//next I try this code for delete 2 and insert new one
a.erase(2);//this code does work nothing !!!
//next I try to find 2 and delete it
HashTable::iterator i;
i = a.find(2);//this code return end, and does not work!!!
a.erase(i);//cause error
//but when I write this code, it works!!!
i=a.begin();
a.erase(i);
//and finally i write this code
for (i = a.begin(); i!=a.end(); ++i)
{
if (i->first == 2)
break;
}
if (i!= a.end())
a.erase(i);
//and this code work
ですが、私は私のデータを検索したい場合は、私は、配列を使用する理由私はアクセスすることはできません、modityをhash_mapし、(1)Oでhash_mapから削除しません 私の間違いは何ですか、そして、初期化段階で多くの値変更を伴う私のプログラムのためには、どのハッシュ構造が速いのですか?それは私にそれにいくつかのチュートリアルを与えることができる場合、私に適したGoogleのsparse_hashです。任意のヘルプ
これは正解です。 hash_compare関数オブジェクトは、要素_の__相対順序を決定するために使用されます。 minaのコードを 's1 == s2'から' s1'はデフォルトですので、hash_compare関数を指定しないとminaの問題も修正されます。 –
Blastfurnace