unordered_set与提升和标准的差异

difference on unordered_set from boost and standard

本文关键字:标准 set unordered      更新时间:2023-10-16

我正在尝试将 boost 和 standard 中的unordered_set用于应用程序,目的是找到位置,即此集合中某些元素的索引。结果之间存在细微差异。boost中的元素根据这个简单的程序被反转。问题出在哪里?

简单的"假设"代码:

#include <iostream>
#include <iterator>
#include <unordered_set>
#include <boost/unordered_set.hpp>
//using boost::unordered_set;
using std::unordered_set;
using std::distance;
int main()
{
  unordered_set<int> Set;
  int sz = 10;
    for(int k=0;k<sz;k++)
        Set.insert(k);
  unordered_set<int>::iterator ind_searched = Set.find(8);
  unordered_set<int>::size_type indx = distance( Set.begin(),
                                                 ind_searched );
  std::cout << " Index of element is "
            << indx << std::endl;
  return 0;
}

有了提升,我得到了

Index of element is 1

并且使用标准unordered_set我得到

Index of element is 8

我编译两者

g++ sgi_stl_1.cc -I /home/utab/external_libraries/boost_1_48_0/ -std=c++0x

你不应该假设任何关于unordered_mapunordered_set、它们的multi对应项或等价物或hash_sethash_maps的实现中的排序。考虑将元素存储为完全实现定义的位置,并且容易随时间变化。排序不仅在boostC++11标准之间有所不同,而且在不同的硬件平台和不同C++实现之间也会有所不同。任何依赖于特定排序的代码都是有缺陷的。所以,哟回答你的问题

问题出在哪里?

问题仅在于假设某些数据在无序数据结构中排序。