boost::multi_index_container
enables the construction of containers maintaining one or more indices with different sorting and access semantics like relational database.
And I use boost::multi_index_container
with composite key to handle something like this:
struct Person {
Person(int id, string name):
m_id(id),
m_name(name)
{
}
int m_id;
string m_name;
};
typedef multi_index_container<
Person,
indexed_by<
ordered_unique<
member<Person, int, &Person::m_id>
>,
ordered_unique<
composite_key<
Person,
member<Person, string, &Person::m_name>,
member<Person, int, &Person::m_id>
>
>
>
> Roster;
int main()
{
Roster r;
r.insert(Person(1, "Tom"));
r.insert(Person(2, "Jack"));
r.insert(Person(3, "Tom"));
r.insert(Person(4, "Leo"));
/* The distinct count of name must be 3, and how to get it? */
}
Is that any way to get the distinct count of the non-unique index key in boost::multi_index_container
like relational database? And how to get the distinct count of first key(Person::name
) of Roster
composite key(Person::m_name
, Person::m_id
)?
THX!
EDIT: Or is it that a way to just iterate the distinct first key? So that we can get the distinct count of first key.
Another possibilty, probably faster if there are many elements with the same key, involves hopping through the index using
upper_bound
:Live On Coliru