Boost logo

Boost Users :

Subject: Re: [Boost-users] [Multi-Index] Categorised Container
From: Joaquin M Lopez Munoz (joaquin_at_[hidden])
Date: 2011-04-30 08:57:01

Hossein Haeri <powerprogman <at>> writes:

> Dear all,
> I have an application where something we call a "categorised container"
> with colleagues comes very hand. We do currently have a working version
> and I am looking to see whether it's possible to do some refactoring on
> that using Multi-Index. Here is an exemplified explanation of what
> constitutes a decision-making point for me. I would be pleased if anyone
> here could tell me whether this facility is available under Multi-Index:
> multi_index_container
> <
>   T
>   hashed_non_unique<...>,
>   ordered_non_unique<...>
> > my_container;
> Now, is there anyway I can get all elements of my_container which
> have a certain hash and sorted according to the second index?

Short answer: no, not in an efficient manner. If you use
equal_range on some particular key for the first index, elements
retrieved won't be ordered accoording to the second index. You
need, for instance, to sort the retrieved range externally.

Longer answer: if you replace the first index with an ordered
index with a composite key you can get the behavior you're after:

> my_container;

Now, if you do an equal_range on a category elements will
indeed be sorted by the second member. Whether this solution
is OK for your problem (having replaced hashing by sorting)
is for you to decide, of course.

More in on composite keys on

Hope this helps,

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at