Accessing an element of multi_index_container using iterator_to
First of all, i'm a programmer with no much experience using Boost Libraries. I'm using Boost for academic purposes and here's my problem. I simply want to access an element of a multi_index_container, by using the iterator_to iterator. the container has entries that are <word(string), word_id(int), tf_idf(float)> and is indexed by unique order for the first two values :word and word_id. *Declaration of my container is below:* *struct counterpart { string word; int id; float tf_idf; counterpart(string word_, int id_) { word = word_; id = id_; tf_idf = 0.0; } }; struct word_name{}; //or word char struct word_id{}; struct word_tf_idf{}; 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; int word_count_id = 1;
From main, I insert some "entries" like below. *This a test main* *int main(void){
//insert to word_set pair<word_index::iterator, bool> word_pair; if((word_pair = word_set.insert(counterpart("apple", word_count_id))).second) //word does not exist word_count_id++; }* I want to access the entry inserted, as i could do with a map. ex map["apple"] The reason i do this, is because i want to get the value of a unique word_id, by accessing the name of the word and a unique word name by accessing a word_id. This of course is not possible, so i have to use iterator_to. But i cannot think how to do this. Should i prepeare a pair as donne when inserting? Also how a value or an entire entry is returned? In which form? Thank you all and I hope you can help me! Andrew Kokkalis undergraduate student of Informatics and Telecomunications University of Athens, Greece
2010/2/22 Andrew Kokkalis <andrewkokkalis@gmail.com>:
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_indi... 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
participants (2)
-
Andrew Kokkalis
-
Dominique Devienne