Okay. Here's the reproducer Live On Coliru
Output is indeed
2 "John" 40
4 "John" 57
5 "John" 58
6 "John" 22
Now to have it also (note the choice of words) ordered by age, you can use a composite key. So instead of:
bmi::ordered_non_unique<
bmi::tag<struct name>,
bmi::member<employee, std::string, &employee::name>
>,
Use
bmi::ordered_non_unique<
bmi::tag<struct name_age>,
bmi::composite_key<employee,
bmi::member<employee, std::string, &employee::name>,
bmi::member<employee, int, &employee::age>
>
>,
And now you can
for (employee const& emp : boost::make_iterator_range(es.get<name_age>().equal_range("John"))) {
std::cout << emp.id << " " << std::quoted(emp.name) << " " << emp.age << "\n";
}
Prints
6 "John" 22
2 "John" 40
4 "John" 57
5 "John" 58
Full sample
Live On Coliru
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/range/iterator_range.hpp>
namespace bmi = boost::multi_index;
struct employee {
int id;
std::string name;
int age;
};
typedef bmi::multi_index_container<
employee,
bmi::indexed_by<
bmi::ordered_unique<
bmi::tag<struct id>,
bmi::member<employee, int, &employee::id>
>,
bmi::ordered_non_unique<
bmi::tag<struct name>,
bmi::member<employee, std::string, &employee::name>
>,
bmi::ordered_non_unique<
bmi::tag<struct name_age>,
bmi::composite_key<employee,
bmi::member<employee, std::string, &employee::name>,
bmi::member<employee, int, &employee::age>
>
>,
bmi::ordered_non_unique<
bmi::tag<struct age>,
bmi::member<employee, int, &employee::age>
>
> > employee_set;
#include <iostream>
#include <iomanip>
int main() {
employee_set es {
{0, "Joe", 31},
{1, "Robert", 27},
{2, "John", 40},
{3, "Albert", 20},
{4, "John", 57},
{5, "John", 58},
{6, "John", 22},
};
std::cout << "name index:\n";
for (employee const& emp : boost::make_iterator_range(es.get<name>().equal_range("John"))) {
std::cout << emp.id << " " << std::quoted(emp.name) << " " << emp.age << "\n";
}
std::cout << "name_age index:\n";
for (employee const& emp : boost::make_iterator_range(es.get<name_age>().equal_range("John"))) {
std::cout << emp.id << " " << std::quoted(emp.name) << " " << emp.age << "\n";
}
}
Prints
name index:
2 "John" 40
4 "John" 57
5 "John" 58
6 "John" 22
name_age index:
6 "John" 22
2 "John" 40
4 "John" 57
5 "John" 58