CPP 字符串的字符串标记器?
我想对 CPP 字符串使用字符串分词器,但我能找到的只是 Char*。 CPP 字符串有类似的吗?
I want to use string Tokenizer for CPP string but all I could find was for Char*.
Is there anything similar for CPP string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
“令牌”是什么意思?如果它是由任何空格分隔的内容,则字符串流就是您想要的:
或者,如果您正在寻找某个单个分隔字符,您可以替换
iss>>token
与 std::getline(iss,token,sep_char) 。如果有多个字符可以充当分隔符(并且不是空格),则为
std::string::find_first()
和std::string::substr 的组合()
应该可以。What do you mean by "token"? If it's something separated by any whitespace, the string streams is what you want:
Alternatively, if your looking for a a certain single separating character, you can replace
iss>>token
withstd::getline(iss,token,sep_char)
.If it's more than one character that can act as a separator (and if it's not whitespaces), a combinations of
std::string::find_first()
andstd::string::substr()
should do.您可以按照 chubsdad 的说明进行操作或使用 boost tokenizer : http: //www.boost.org/doc/libs/1_44_0/libs/tokenizer/tokenizer.htm
如果您害怕 Boost,那么自己做并不那么复杂。
You can do as said by chubsdad or use boost tokenizer : http://www.boost.org/doc/libs/1_44_0/libs/tokenizer/tokenizer.htm
Doing it by yourself is not so complicated if you're affraid by Boost.
您应该看看 Boost Tokenizer
You should take a look at Boost Tokenizer
查看 STL 算法,例如 find_first_of、find_first_not_of 等来创建自定义算法。
Check out STL algos like find_first_of, find_first_not_of and so on to create a custom one.
试试我在某处找到的这个片段(也许就在这里?):
Try this snippet I found somewhere (maybe even around here?):