我boost::tokenizer
用来读取类似 CSV 的文件。我将令牌存储在std::vector
. 它运作良好,但我只想boost::iterator
为每个令牌存储一个。
我试过:
#include <string>
#include <boost/tokenizer.hpp>
#include <boost/range/iterator_range.hpp>
typedef std::string::const_iterator string_iter;
typedef boost::iterator_range<string_iter> string_view;
int main(){
std::string line;
std::vector<string_view> contents;
boost::tokenizer<boost::escaped_list_separator<char>, string_iter, string_view> tok(line.begin(), line.end());
contents.assing(tok.begin(), tok.end());
}
但它无法编译:
/usr/include/boost/token_functions.hpp:在 'bool boost::escaped_list_separator::operator()(InputIterator&, InputIterator, Token&) 的实例化中 [with InputIterator = __gnu_cxx::__normal_iterator >; 令牌 = boost::iterator_range<__gnu_cxx::__normal_iterator >>; 字符 = 字符;Traits = std::char_traits]': /usr/include/boost/token_iterator.hpp:70:11: 需要来自'void boost::token_iterator::initialize() [with TokenizerFunc = boost::escaped_list_separator; 迭代器 = __gnu_cxx::__normal_iterator >; 类型 = boost::iterator_range<__gnu_cxx::__normal_iterator >>]' /usr/include/boost/token_iterator.hpp:77:63: 来自 'boost::token_iterator::token_iterator(TokenizerFunc, Iterator, Iterator) [with TokenizerFunc = boost::escaped_list_separator; 迭代器 = __gnu_cxx::__normal_iterator >; 类型 = boost::iterator_range<__gnu_cxx::__normal_iterator >>]' /usr/include/boost/tokenizer.hpp:86:53: 来自 'boost::tokenizer::iter boost::tokenizer::begin() 常量[与 TokenizerFunc = boost::escaped_list_separator; 迭代器 = __gnu_cxx::__normal_iterator >; 类型 = boost::iterator_range<__gnu_cxx::__normal_iterator >>; boost::tokenizer::iter = boost::token_iterator, __gnu_cxx::__normal_iterator >, boost::iterator_range<__gnu_cxx::__normal_iterator >> >]' /home/wichtounet/dev/gooda-to-afdo-converter/src/ gooda_reader.cpp:58:37: 从这里需要 /usr/include/boost/token_functions.hpp:187:16: 错误: 'tok += (& next)->__gnu_cxx::__normal_iterator 中的 'operator+=' 不匹配<_Iterator, _Container>::operator* >()' /usr/include/boost/token_functions.hpp:193:11: 错误:'tok += (& next)->__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* >()' /usr/include/boost/token_functions.hpp 中的 'operator+=' 不匹配:在实例化中'void boost::escaped_list_separator::do_escape(iterator&, iterator, Token&) [with iterator = __gnu_cxx::__normal_iterator >; 令牌 = boost::iterator_range<__gnu_cxx::__normal_iterator >>; 字符 = 字符;Traits = std::char_traits]': /usr/include/boost/token_functions.hpp:176:11: 来自'bool boost::escaped_list_separator::operator()(InputIterator&, InputIterator, Token&) [with InputIterator = __gnu_cxx: :__normal_iterator >; 令牌 = boost::iterator_range<__gnu_cxx::__normal_iterator >>; 字符 = 字符;特征 = std::char_traits]' /usr/include/boost/token_iterator.hpp:70:11: 'void boost::token_iterator::initialize() [with TokenizerFunc = boost::escaped_list_separator; 迭代器 = __gnu_cxx::__normal_iterator >; 类型 = boost::iterator_range<__gnu_cxx::__normal_iterator >>]' /usr/include/boost/token_iterator.hpp:77:63: 来自 'boost::token_iterator::token_iterator(TokenizerFunc, Iterator, Iterator) [with TokenizerFunc = boost::escaped_list_separator; 迭代器 = __gnu_cxx::__normal_iterator >; 类型 = boost::iterator_range<__gnu_cxx::__normal_iterator >>]' /usr/include/boost/tokenizer.hpp:86:53: 来自 'boost::tokenizer::iter boost::tokenizer::begin() 常量[与 TokenizerFunc = boost::escaped_list_separator; 迭代器 = __gnu_cxx::__normal_iterator >; 类型 = boost::iterator_range<__gnu_cxx::__normal_iterator >>; 升压::分词器::
我也只是尝试使用 自己计算两个迭代器boost::token_iterator
,但到目前为止我还没有成功。
是否有解决方案只获取每个令牌的迭代器范围而不是字符串以节省一些性能?