在 Stl Hash_map 中查找密钥

发布于 2024-10-07 03:30:04 字数 1425 浏览 0 评论 0原文

我是 C++ 初学者,对哈希表有一些问题。我的程序需要一个哈希表结构。首先我使用 boost unordered_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 

但是如果我想搜索我的数据,我使用数组而不是 hash_map,为什么我无法使用 o(1) 从 hash_map 访问、修改和删除 我的错误是什么,哪种哈希结构对于我的程序来说是快速的,在初始化阶段有很多值修改。 google稀疏哈希适合我吗,如果适合,可以给我一些教程。 感谢您的帮助

I am beginner in c++ and have some problem with hash table. I need a Hash table structure for my program. first I use boost unordered_map. it have all things that I need, but it make my program so slow. then I want to test stl hash_map, but I can't do all thing that I need. this is my first code ( this is sample)

#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 

but if i want to search over my data, i use array not hash_map, why I can't access, modity and delete from hash_map with o(1)
what is my mistake, and which hash structure is fast for my program with many value modification in initializing phase. is google sparse_hash suitable for me, if it is, can give me some tutorial on it.
thanks for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云之铃。 2024-10-14 03:30:04

您可以查看:http://msdn.microsoft.com /en-us/library/525kffzd(VS.71).aspx

我认为 stdext::hash_compare< int, eqstr > 导致了这里的问题。尝试将其删除。

哈希映射的另一个实现是 std::tr1::unordered_map。但我认为各种哈希映射实现的性能是相似的。您能否详细说明一下 boost::unordered_map 有多慢?你是怎么用的?做什么的?

You may look at: http://msdn.microsoft.com/en-us/library/525kffzd(VS.71).aspx

I think stdext::hash_compare< int, eqstr > is causing the problems here. Try to erase it.

Another implementation of a hash map is std::tr1::unordered_map. But I think that performance of various hash map implementation would be similar. Could you elaborate more about how slow the boost::unordered_map was? How did you use it? What for?

清风疏影 2024-10-14 03:30:04

哈希映射有很多不同的种类,许多开发人员仍然编写自己的哈希映射,因为与通用哈希映射相比,根据自己的特定用途编写的哈希映射通常可以获得更高的性能,并且人们倾向于当他们想要真正高性能时使用哈希。

你需要考虑的第一件事是你真正需要做什么以及你真正需要多少性能,然后确定现成的东西是否可以满足你的需求,或者你是否需要自己编写一些东西。

例如,如果您从不删除元素,而只是写入一次然后不断查找,那么您通常可以重新散列以减少您获得的实际集合的冲突:设置时间更长,但查找速度更快。

如果您删除元素,那么在编写自己的元素时会出现问题,因为它不足以“清空”条目,因为另一个元素可能已经跨过您的元素作为其碰撞过程的一部分,现在如果您查找该元素,它会放弃一旦它达到你的空值,就会显示为“未找到”。

There are so many different varieties of hash map and many developers still write their own because you can so much more often get a higher performance in your own one, written to your own specific use, than you can from a generic one, and people tend to use hash when they want a really high performance.

The first thing you need to consider is what you really need to do and how much performance you really need, then determine whether something ready-made can meet that or whether you need to write something of your own.

If you never delete elements, for example, but just write once then constantly look-up, then you can often rehash to reduce collisions for the actual set you obtain: longer at setup time but faster at lookup.

An issue in writing your own will occur if you delete elements because it is not enough to "null" the entry, as another one may have stepped over yours as part of its collision course and now if you look that one up it will give up as "not found" as soon as it hits your null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文