简体   繁体   English

在Stl中查找键Hash_map

[英]Find Key in Stl Hash_map

I am beginner in c++ and have some problem with hash table. 我是C ++的初学者,哈希表有一些问题。 I need a Hash table structure for my program. 我的程序需要一个哈希表结构。 first I use boost unordered_map. 首先,我使用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. 然后我想测试stl hash_map,但是我不能做所有我需要的事情。 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. 但是,如果我要搜索数据,则使用数组而不是hash_map,为什么我不能使用o(1)访问,修改和从hash_map中删除,这是我的错误,以及哪种哈希结构对我的程序来说具有很多价值,它是快速的在初始化阶段进行修改。 is google sparse_hash suitable for me, if it is, can give me some tutorial on it. google sparse_hash是否适合我,如果可以,可以给我一些教程。 thanks for any help 谢谢你的帮助

You may look at: http://msdn.microsoft.com/en-us/library/525kffzd(VS.71).aspx 您可能会看到: http : //msdn.microsoft.com/zh-cn/library/525kffzd(VS.71).aspx

I think stdext::hash_compare< int, eqstr > is causing the problems here. 我认为stdext::hash_compare< int, eqstr >引起了这里的问题。 Try to erase it. 尝试擦除它。

Another implementation of a hash map is std::tr1::unordered_map . 哈希映射的另一种实现是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? 您能否详细说明一下boost :: unordered_map有多慢? How did you use it? 您是如何使用它的? What for? 做什么的?

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. 如果您删除元素是因为它不足以“清空”该条目,则这将导致编写自己的问题,因为另一条目可能会在碰撞过程中跨过您的条目,而现在,如果您放弃它就会放弃到达空值后立即显示为“未找到”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM