我试图在应用程序中使用boost和standard的unordered_set,目的是找到这个位置,即该集合中某些元素的索引。结果之间存在细微差别。根据这个简单的程序,提升中的元素是相反的。问题在哪里?
简单的“假设”代码:
#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
答案 0 :(得分:6)
您不应该对unordered_map
,unordered_set
,他们的multi
对应人或同等人或hash_set
或hash_maps
的任何实施中的排序做任何假设。将元素存储的位置视为完全实现定义,并且易于及时更改。排序不仅会在boost
和C++11
标准之间变化,而且会在不同的硬件平台之间以及不同的C ++实现之间变化。任何依赖某种顺序的代码都是有缺陷的。那么,你回答你的问题
问题出在哪里?
问题仅在于在无序数据结构中假设某些数据排序。