访问增强分词器的元素

Access elements of boost tokenizer

本文关键字:元素 分词 增强 访问      更新时间:2023-10-16

我正在尝试使用 boost 将文件的列分配给 std::map。 我想将每行中的元素 0 分配给索引,将元素 2 分配给值。 有没有办法在没有迭代器的情况下做到这一点?addr_lookup行不起作用。

#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;
  }
}

您正在尝试使用 + 推进迭代器,这是不可能的

用:

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

或者简单地说,

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