Using multi-index containers
In the previous recipe, we made some kind of vocabulary, which is good when we need to work with pairs. But, what if we need much more advanced indexing? Let's make a program that indexes persons:
struct person {
std::size_t id_;
std::string name_;
unsigned int height_;
unsigned int weight_;
person(std::size_t id, const std::string& name,
unsigned int height, unsigned int weight)
: id_(id)
, name_(name)
, height_(height)
, weight_(weight)
{}
};
inline bool operator < (const person& p1, const person& p2) {
return p1.name_ < p2.name_;
}We will need a lot of indexes, for example, by name, ID, height, and weight.
Getting ready
Basic knowledge about standard library containers and unordered maps is required.
How to do it...
All the indexes can be constructed and managed by a single Boost.Multiindex container.
- To do so, we need a lot of includes:
#include <iostream...