C++ 的 split 函数
C++有没有类似Java的split类型函数?我知道忽略,但我不太明白它,以及它如何适用于我的情况。
我的输入是:
{
item = ball
book = lord of the rings
movie = star wars
}
我给出的输入是
=
并且我必须将两者存储在不同的字符串或整数中(取决于值,例如,如果它是数字,则使用整数)。
Is there a split type function for C++ similar to Java? I know of ignore, but I don't quite understand it, and how it'll work for my case.
My input is:
{
item = ball
book = lord of the rings
movie = star wars
}
My input given is an <attribute>
= <value>
and I have to store the two in different strings, or integers (depending on the value, for example, if its a number, use an integer).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 Boost::tokenizer 因为它可以满足您的需求去做。从手册中:
Use Boost::tokenizer as it does what you want to do. From the manual:
使用strtok(): http://www.cplusplus.com/reference/clibrary/ cstring/strtok/。
只需知道它不可重入,因为它使用内部静态变量,所以不要在嵌套循环或类似的东西中调用它两次。
和编辑:
这是一个非常酷的解决方案,它将用空格标记整个字符串 - 你必须在=之后将值重新处理在一起,但它会很好地教你STL:)
在 C++ 中拆分字符串?
Use strtok(): http://www.cplusplus.com/reference/clibrary/cstring/strtok/.
Just know that its not re-entrant because it uses an internal static variable, so don't call it twice in nested loops or anything like that.
and EDIT:
This is a very cool SO solution that would tokenize the whole string by spaces - you'd have to process the values back together after the = but it would teach you STL well :)
Split a string in C++?