简体   繁体   English

hash_map会自动排序[C ++]吗?

[英]Does hash_map automatically sort [C++]?

In the below code, hash_map is automatically sorting or maybe inserting elements in a sorted order. 在下面的代码中,hash_map自动排序或者按排序顺序插入元素。 Any ideas why it does this?? 任何想法为什么这样做? Suggestions Please?? 建议请?? This is NOT a homework problem, trying to solve an interview question posted on glassdoor dot com. 这不是一个家庭作业问题,试图解决在glassdoor dot com上发布的面试问题。

#include <iostream>
#include <vector>
#include <ext/hash_map>
#include <map>
#include <string.h>
#include <sstream>

using namespace __gnu_cxx;
using namespace std;

struct eqstr
{
  bool operator()(int i, int j) const
  {
    return i==j;
  }
};
typedef hash_map<int, int, hash<int>, eqstr> myHash;
int main()
{
    myHash array;
    int inputArr[20] = {1,43,4,5,6,17,12,163,15,16,7,18,19,20,122,124,125,126,128,100};

    for(int i=0;i<20;i++){
        array[inputArr[i]] = inputArr[i]; //save value
    }
    myHash::iterator it = array.begin();
    int data;
    for (; it != array.end(); ++it) {
        data =  it->first;
        cout << ":: " << data;
    }
}

//!Output ::: 1:: 4:: 5:: 6:: 7:: 12:: 15:: 16:: 17:: 18:: 19:: 20:: 43:: 100:: 122:: 124:: 125:: 126:: 128:: 163

Hash map will not automatically sort your data. 哈希映射不会自动数据进行排序 In fact the order is unspecified, depending on your hash function and input order. 实际上,订单是未指定的,具体取决于您的哈希函数和输入顺序。 It is just that in your case the numbers turns out are sorted. 只是在你的情况下,数字结果是排序的。

You may want to read about hash table for how this container stores the data. 您可能希望阅读有关此容器如何存储数据的哈希表

A clear counter example can be created by replacing that 100 with 999999999. The result is 通过用999999999替换100可以创建一个清晰的计数器示例。结果是

:: 1:: 4:: 5:: 6:: 7:: 12:: 15:: 16:: 17:: 18:: 19:: 20:: 999999999:: 43:: 122:: 124:: 125:: 126:: 128:: 163

(The actual reason is the hash_map's bucket_count is 193 and the hash function of int is an identity function, so any numbers below 193 will appear sorted.) (实际原因是hash_map的bucket_count是193,而int的哈希函数是一个标识函数,因此任何低于193的数字都会出现排序。)

A hash map may appear to be sorted based on a few factors: 哈希映射可能基于以下几个因素进行排序:

  • The hash function returns values that are in the same order as the input, for the range of input that you are providing. 对于您提供的输入范围,散列函数返回与输入顺序相同的值。
  • There are no hash collisions. 没有哈希冲突。

There are no guarantees that the results will be sorted, it's only a coincidence if they come out that way. 无法保证结果将被排序,如果它们以这种方式出现,那只是巧合。

Think about how the hash function operates. 想想哈希函数如何运作。 A hash is always a function f:input->output that maps the input set I into a (usually smaller) output set O so that the input set is approximately uniformly distributed across the output set. 散列总是函数f:input-> output ,它将输入集I映射到(通常更小的)输出集O中,以便输入集大致均匀地分布在输出集上。

There is no requirement that order should be preserved; 没有要求保留秩序; in fact, it's unusual that it would be preserved, because (since the output set is smaller) there will be values *i,j*that have the same hash. 实际上,保留它是不寻常的,因为(因为输出集较小),将有值* i,j *具有相同的散列。 This is called a collision . 这称为碰撞

On the other hand, there's no reason it shouldn't. 另一方面,没有理由不应该这样做。 IN fact, it can be proven that there will always exist at least one sequence that will preserve the order. 实际上,可以证明至少有一个序列可以保留顺序。

But there's another possibility: if ALL the values collide, then they get stored in some other kind of data structure, like a list. 但还有另一种可能性:如果所有值都发生碰撞,那么它们就会存储在其他类型的数据结构中,就像列表一样。 It may be that these all collidge, and the other structure imposes order. 可能是这些所有的collidge,而另一个结构强加了秩序。

Three possibilities: hash_map happens to sort that particular sequence, or hash_map is actually implimented as an array, or the values collidge and the implementation stores collisions in a way that gives a sorted order. 三种可能性: hash_map恰好对特定序列进行排序,或者hash_map实际上作为一个数组进行了修饰,或者值collidge和实现以一种给出排序顺序的方式存储冲突。

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

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