motam79
motam79

Reputation: 3824

Boost:Multi-index: How to iterate over all results matching a non-unique ordered index?

I have a Boost multi-index container for storing MyClass members. It has a unique (first_field) and non-unique (second field) indices:

typedef multi_index_container<
MyClass,
indexed_by<
        ordered_unique<member<MyClass, std::string, MyClass.first_field>>,
        ordered_non_unique<member<MyClass &, std::string, MyClass.second_field>>>
> MyClass_Set;

If I search the container by the second index:

auto it = container.get<1>().find("second_field_value_to_be_searched);

I get a const iterator back. How do I iterate over ALL elements in the container that matches the above predicate?

Upvotes: 2

Views: 1639

Answers (1)

sehe
sehe

Reputation: 392911

So, use equal_range instead:

auto r = container.get<1>().equal_range("second_field_value_to_be_searched");

This yields a pair of iterators. You can iterate them as usual, or wrap them in an iterator range:

for (auto& record : boost::make_iterator_range(r)) {
}

Upvotes: 5

Related Questions