unordered_set与提升和标准的差异
difference on unordered_set from boost and standard
我正在尝试将 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_map
、unordered_set
、它们的multi
对应项或等价物或hash_set
或hash_maps
的实现中的排序。考虑将元素存储为完全实现定义的位置,并且容易随时间变化。排序不仅在boost
和C++11
标准之间有所不同,而且在不同的硬件平台和不同C++实现之间也会有所不同。任何依赖于特定排序的代码都是有缺陷的。所以,哟回答你的问题
问题出在哪里?
问题仅在于假设某些数据在无序数据结构中排序。
相关文章:
- 使用CMake检测支持的C++标准
- 如何理解C++标准N3337中的expr.const.cast子句8
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 编译标准库类型
- 为什么我无法更改"set<set>"循环中的值<int>
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 对于set上的循环-获取next元素迭代器
- 编译器如何在使用SFINAE的函数和标准函数之间确定两者是否可行
- 铸造标准::有没有回到原来的类型
- 在声明中合并两个常量"std::set"(不是在运行时)
- 标准 N3337 5.2.10 第 7 条中的C++"类型"是什么意思?
- 有没有办法对std::unordered_set、std::unrdered_map、std::set、std::map
- this_thread::sleep_for和计时时钟之间的关系是否由C++11标准指定
- 标准库类型的赋值运算符的引用限定符
- 如何将标准::矢量插入具有自定义排序功能的 std::set 中
- 标准上的 OMP 和并行操作::set<...>::迭代器
- C 标准的哪一部分要求set ::擦除呼叫灾难源
- 标准重载运算符<:设置(英语:SET)
- 使用标准c++库vector作为底层实现创建Set模板类
- 标准::set 中的自定义函子