Question

sorry if this is a newB question, please conider the following code:

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/tokenizer.hpp>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string>

using boost::multi_index_container;
using namespace boost::multi_index;


typedef multi_index_container<
  std::string,
  indexed_by<
    sequenced<>,
    ordered_non_unique<identity<std::string> >
  >
> text_container;


typedef boost::tokenizer<boost::char_separator<char> > text_tokenizer;

int main()
{
  std::string text=
    "Alice was getting very tired of sitting by her sister";

  text_container tc;
  text_tokenizer tok(text,boost::char_separator<char>(" \t\n.,;:!?'\"-"));
  std::copy(tok.begin(),tok.end(),std::back_inserter(tc));
  int i=0;
  for(text_container::iterator bb=tc.begin();bb!=tc.end();bb++,i++)
//    std::cout << *bb << std::endl;
      std::cout << tc[i] << std::endl;
  return 0;
}

I would like to access, for example, the 10th element in the container. do I still have to use an iterator? or is there away to access a specific sequenced element in array-like fashion(or any other way...please suggest) Appreciate your help vahid

Was it helpful?

Solution

You can specify a random access index for the multi index by changing the line sequenced<>, to random_access<>, (you'll need to #include <boost/multi_index/random_access_index.hpp>). This will allow you to remove the iterator in the for loop.

For further details, see the documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top