整数分词器
我知道有字符串标记器,但是有“int 标记器”吗?
例如,我想拆分字符串“12 34 46”并具有:
列表[0]=12
列表[1]=34
列表[2]=46
特别是,我想知道 Boost::Tokenizer 是否这样做。 尽管我找不到任何不使用字符串的示例。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定您是否可以在不使用 string 或 char* 的情况下执行此操作,因为您必须将数字和空格放入同一组中...
i am not sure if you can do this without using string or char* because you have to but both numbers and spaces into same set...
您正在寻找的是 2 个单独的操作。 首先对字符串进行标记,然后将每个标记转换为 int。
What you're looking for is 2 separate actions. First tokenize the string, then convert each token to an int.
是的,有:使用流,例如
stringstream
:或者,您也可以将 STL 算法和/或迭代器适配器与构造函数结合使用:
Yes there is: use a stream, e.g. a
stringstream
:Alternatively, you can also use STL algorithms and/or iterator adapters combined with constructors:
C++ 字符串工具包库 (StrTk) 针对您的问题提供了以下解决方案:
更多示例可以在此处找到。
注意:解析过程非常 快速高效,让基于 stdlib 和 boost 的解决方案相形见绌。
The C++ String Toolkit Library (StrTk) has the following solution to your problem:
More examples can be found Here
Note: The parsing process is EXTREMELY fast and efficient, putting stdlib and boost based solutions to shame.