字符串流到向量
我想知道从 std::stringstream
写入 vector
的最佳方法是什么。
以下是 stringstream
中内容的示例: "31 #00 532 53 803 33 534 23 37"
这就是我得到的:
int buffer = 0;
vector<int> analogueReadings;
stringstream output;
while(output >> buffer)
analogueReadings.push_back(buffer);
然而,似乎发生的是,它读取第一件事,然后它到达 #00
code> 并返回 0
因为它不是数字。
理想情况下,我想要的是,它到达 #
然后跳过所有字符直到下一个空格。这可以通过标志或其他东西实现吗?
谢谢。
I'm wondering what the best way to write from an std::stringstream
into a vector<int>
.
Here's an example of what's in the stringstream
:"31 #00 532 53 803 33 534 23 37"
Here's what I've got:
int buffer = 0;
vector<int> analogueReadings;
stringstream output;
while(output >> buffer)
analogueReadings.push_back(buffer);
However what seems to happen is, it reads the first thing, then it gets to #00
and returns 0
because it's not a number.
Ideally, what I want is, it gets to a #
and then just skips all characters until the next whitespace. Is this possible with flags or something?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要测试是否有号码。使用这里的答案:
如何确定是否字符串是 C++ 中的数字?
结果是 31 532 99 53 803 33 534 23 37
另外,使用的重要缺点这里描述了这样的词汇转换:
如何在 C++ 中将字符串解析为 int?< /a> ,其中给出了
tringstream(tmpbuff)>>num
的替代方案。例如04hello变成4,7.4e55变成7。还有可怕的下溢和下溢问题。 André Caron 的干净解决方案已转换
为
我的系统。请注意,77 也丢失了!
You need to test if you got a number or not. use the answer from here:
How to determine if a string is a number with C++?
the result is 31 532 99 53 803 33 534 23 37
Also, important drawbacks of using lexical casts like this is described here:
How to parse a string to an int in C++? , where an alternative to
tringstream(tmpbuff)>>num
is given.For example 04hello becomes 4 and 7.4e55 becomes 7. There are also terrible problems with underflow and underflow. The clean solution by André Caron converts
into
on my system. Note that also 77 is missing!
无循环版本:
No loops version:
我认为最好的方法是
I think the best method would be to