Int分词器 [英] Int tokenizer

查看:132
本文介绍了Int分词器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有字符串tokenizer,但是有一个int tokenizer?



例如,我想拆分字符串12 34 46


list [0] = 12



list [1] = 34



list [2] = 46


如果Boost :: Tokenizer这样做。虽然我找不到没有使用字符串的例子。

解决方案

是的有: a stringstream

  stringstream sstr(12 34 46); 
int i
while(sstr>> i)
list.push_back(i);或者,您也可以使用STL算法和/或迭代器适配器与构造函数组合:


 矢量< int> list = vector< int>(istream_iterator< int>(sstr),istream_iterator< int>()); 


I know there are string tokenizers but is there an "int tokenizer"?

For example, I want to split the string "12 34 46" and have:

list[0]=12

list[1]=34

list[2]=46

In particular, I'm wondering if Boost::Tokenizer does this. Although I couldn't find any examples that didn't use strings.

解决方案

Yes there is: use a stream, e.g. a stringstream:

stringstream sstr("12 34 46");
int i;
while (sstr >> i)
    list.push_back(i);

Alternatively, you can also use STL algorithms and/or iterator adapters combined with constructors:

vector<int> list = vector<int>(istream_iterator<int>(sstr), istream_iterator<int>());

这篇关于Int分词器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆