Umut Tabak
Umut Tabak

Reputation: 1942

difference on unordered_set from boost and standard

I am trying to use unordered_set from boost and standard for an application, the aim is to find the place, namely, the index of certain elements in this set. There is a subtle difference between the results. The elements in boost are reversed according to this simple program. Where is the problem?

simple 'what-if' code :

#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;
}

With boost I get

Index of element is 1

and with standard unordered_set I am getting

Index of element is 8

I compile both with

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

Upvotes: 2

Views: 2416

Answers (1)

juanchopanza
juanchopanza

Reputation: 227390

You should not assume anything about the ordering inside any implementation of unordered_map, unordered_set, their multi counterparts or equivalent or hash_set or hash_maps. Consider the place where an element is stored as completely implementation defined, and prone to change in time. Ordering will not only vary between boost and the C++11 standard, but between different hardware platforms and between different C++ implementations. Any code that relies a certain ordering is flawed. So, yo answer your question

where is the problem?

the problem is only in assuming some data ordering in an unordered data structure.

Upvotes: 6

Related Questions