Simus
Simus

Reputation: 136

boost multi_index - how to make it ordered-by-insert?

Its definition is below:

typedef boost::multi_index_container<
  X, // the data type stored
  boost::multi_index::indexed_by< 
    boost::multi_index::hashed_unique<
      boost::multi_index::composite_key<
        X,
        boost::multi_index::member<X,std::string,&X::name>,
        boost::multi_index::member<X,std::string,&X::p1>,
        boost::multi_index::member<X,std::string,&X::p2>
      >
    >
  >
> container;

I inserting new items with insert().

But container sorts them with the string keys, not stores inserting order.

I need it be stored.

Upvotes: 1

Views: 257

Answers (1)

Caleth
Caleth

Reputation: 63362

You need to add an index to the definition for each ordering you want to be able to search by.

boost::multi_index::sequenced gives std::list like access

boost::multi_index::random_access gives std::vector like access

Note also that the container you define does not keep things in any particular order, it has std::unordered_set like access for it's only index.

boost::multi_index_container<
  X, // the data type stored
  boost::multi_index::indexed_by< 
    boost::multi_index::random_access,
    boost::multi_index::hashed_unique<
      boost::multi_index::composite_key<
        X,
        boost::multi_index::member<X,std::string,&X::name>,
        boost::multi_index::member<X,std::string,&X::p1>,
        boost::multi_index::member<X,std::string,&X::p2>
      >
    >
  >
> container;

container.index<0>().erase(container.index<0>().begin() + 10) // insertion order
container.index<1>().find(some_value) // unordered

Upvotes: 2

Related Questions