Ross W
Ross W

Reputation: 1370

Access elements of boost tokenizer

I'm trying to assign columns of a file using boost to a std::map. I would like to assign element 0 from each line to the index and element 2 to the value. Is there a way to do this without an iterator? The addr_lookup line does not work.

#include <iostream>
#include <fstream> 
#include <string>
#include <map>
#include <boost/tokenizer.hpp>
#include <boost/lexical_cast.hpp>

int main()
{
  std::ifstream myfile("core_info_lowbits.tab", std::ios_base::in);
  std::string line;
  typedef boost::tokenizer<boost::char_separator<char> >  tokenizer;
  boost::char_separator<char> sep(" ");
  std::map<std::string, unsigned int> addr_lookup;

  while ( std::getline (myfile,line) )
  {
    tokenizer tokens(line, sep);
    //Line below does not work
    addr_lookup[*tokens.begin()] = boost::lexical_cast<unsigned int> (*(tokens.begin()+2));
    for (tokenizer::iterator tok_iter=tokens.begin();
         tok_iter != tokens.end(); ++tok_iter)
          std::cout << *tok_iter << std::endl;
  }
}

Upvotes: 2

Views: 3892

Answers (1)

P0W
P0W

Reputation: 47824

You are trying to advance the iterator using +, which is not possible

Use:

tokenizer::iterator it1,it2= tokens.begin();
it1=it2;
++it2; ++it2;

addr_lookup[*it1] = boost::lexical_cast<unsigned int> (*it2);

Or simply,

tokenizer::iterator it1,it2= tokens.begin();
it1=it2;
std::advance(it2,2);
addr_lookup[*it1] = boost::lexical_cast<unsigned int> (*it2);

Upvotes: 4

Related Questions