Boost logo

Boost Users :

Subject: Re: [Boost-users] Accessing an element of multi_index_container using iterator_to
From: Dominique Devienne (ddevienne_at_[hidden])
Date: 2010-02-22 10:15:41


2010/2/22 Andrew Kokkalis <andrewkokkalis_at_[hidden]>:
> Declaration of my container is below:
> struct counterpart {
>     string word;
>     int id;
>     float tf_idf;
>
>     counterpart(string word_, int id_) {

Prefer an initializer list: counterpart(string word, int id) :
word(word), id(id), tf_idf(0.0) {}

> };
>
> typedef multi_index_container<
>     counterpart,
>     indexed_by<
>         ordered_unique<
>             tag<word_name>, BOOST_MULTI_INDEX_MEMBER(counterpart, string, word)>,
>         ordered_unique<
>             tag<word_id>, BOOST_MULTI_INDEX_MEMBER(counterpart, int, id)>,
>         ordered_non_unique<
>             tag<word_tf_idf>, BOOST_MULTI_INDEX_MEMBER(counterpart, float, tf_idf)> >
>> word_index;
>
> word_index word_set;

Each index of word_index acts/behaves like the corresponding STL
container, and word_index "derives" from its first index so also
behaves like it.

So to search by word (first index), simply do a word_set.find() with a string.
To search by any index, first get a *reference* to that index, e.g.
word_set.get<word_id>(), and then again use it according to its API.
Set operations on ordered indexes are documented here:
http://www.boost.org/doc/libs/1_41_0/libs/multi_index/doc/reference/ord_indices.html#set_operations

Modifying elements is a little different, to allow the multi-index
container to check them against all its indexes and possibly veto
them, with optional commit/rollback semantic.

I suggest you go thru the tutorial where all this is described. --DD


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net