简体   繁体   English

如何使用正则表达式和boost转换迭代器标记和转换c字符串?

[英]How to tokenize and transform a c-string using regex and boost transform iterators?

I try to tokenize a c-string with semicolon separated digits and store them in a vector. 我尝试用分号分隔的数字标记一个c字符串,并将它们存储在向量中。 This is my simplified approach 这是我的简化方法

auto string = "1;2;3;4";
const std::regex separator {";"};
std::cregex_token_iterator t_begin{string, string + strlen(string), separator, -1};
std::cregex_token_iterator t_end{};
auto begin = boost::make_transform_iterator(t_begin, atoi);
auto end = boost::make_transform_iterator(t_end, atoi);
std::vector<int> result{begin, end};

I get the error message: 我收到错误消息:

error: no type named 'type' in 'boost::mpl::eval_if<boost::is_same<boost::iterators::use_default, boost::iterators::use_default>, boost::result_of<const int(std::sub_match<const char*>&)>, boost::mpl::identity<boost::iterator::use_default> >::f_{aka struct boost::result_of<const int(const std::sub_match<const char*>&)>}'
typedef typename f_::type type;

which I don't understand. 我不明白。

std::cregex_token_iterator , when dereferenced, returns a std::sub_match of a corresponding type. std::cregex_token_iterator取消引用后,将返回对应类型的std::sub_match In this case, it's a pair of const char* pointers, so a possible solution is as follows: 在这种情况下,它是一对const char*指针,因此可能的解决方案如下:

auto f = [] (std::csub_match m) { return std::atoi(m.first); };

auto begin = boost::make_transform_iterator(t_begin, f);     
auto end = boost::make_transform_iterator(t_end, f);

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM