在 C++ 中使用 istringstream 将字符串拆分为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5168670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Splitting a string into integers using istringstream in C++
提问by Znorg
I'm trying to use istringstream
to split a simple string into a series of integers:
我正在尝试istringstream
将一个简单的字符串拆分为一系列整数:
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main(){
string s = "1 2 3";
istringstream iss(s);
while (iss)
{
int n;
iss >> n;
cout << "* " << n << endl;
}
}
And i get:
我得到:
* 1
* 2
* 3
* 3
Why is the last element always coming out twice? How to fix it?
为什么最后一个元素总是出现两次?如何解决?
回答by Lightness Races in Orbit
It's coming out twice because your looping is wrong, as explained (indirectly) at http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5(while (iss)
is not dissimilar from while (iss.eof())
in this scenario).
它出现了两次,因为你的循环是错误的,正如在http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5 上(间接地)解释的那样(while (iss)
与while (iss.eof())
这种情况下没有不同) .
Specifically, on the third loop iteration, iss >> n
succeeds and gets your 3
, and leaves the stream in a good state. The loop then runs a fourth time due to this good state, and it's not until the next (fourth) iss >> n
subsequently fails that the loop condition is broken. But before that fourth iteration ends, you still output n
... a fourth time.
具体来说,在第三次循环迭代中,iss >> n
成功并获取您的3
,并使流保持良好状态。由于这种良好状态,循环然后第四次运行,直到下一次(第四次)iss >> n
随后失败,循环条件才被破坏。但是在第四次迭代结束之前,您仍然输出n
......第四次。
Try:
尝试:
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
string s = "1 2 3";
istringstream iss(s);
int n;
while (iss >> n) {
cout << "* " << n << endl;
}
}
回答by Sahil Sareen
Hope this helps:
iss : 1 2 3
Iteration 1
iss : 1 2 3 (Initially)
n=1
iss : 2 3
//* 1 is printed
Iteration 2:
iss : 2 3 (Initially)
n=2
iss : 3
//* 2 is printed
Iteration 3
iss : 3
n=3
iss : ''
Iteration 4
iss : ''
n not changed//Flag set for eofof iss as no further input from stream
iss : ''
希望这会
有所帮助:iss : 1 2 3
Iteration 1
iss : 1 2 3 (Initially)
n=1
iss : 2 3
//* 1 被打印
Iteration 2:
iss : 2 3 (Initially)
n=2
iss : 3
// * 2 被打印
Iteration 3
iss : 3
n=3
iss : ''
Iteration 4
iss : ''
n not changed//为 iss 的eof设置标志,因为没有来自流
iss 的进一步输入:''
And as rightly mentioned by the above post while (iss) is not dissimilar from while (iss.eof()).
Internally, the function(istream::operator>>) accesses the input sequence by first constructing a sentryobject (with noskipws set to false[This means that space is separator and your list will be 1,2,3]). Then (if good[here eof not reached]), it calls num_get::get[Get the next integer] to perform both the extraction and the parsing operations, adjusting the stream's internal state flags accordingly. Finally, it destroys the sentry object before returning.
正如上面的帖子正确提到的,while (iss) 与 while (iss.eof()) 没有什么不同。
在内部,函数(istream::operator>>) 通过首先构造一个哨兵对象来访问输入序列(将 noskipws 设置为 false [这意味着空格是分隔符,您的列表将是 1,2,3])。然后(如果良好[此处 eof 未达到]),它调用num_get::get[Get the next integer] 来执行提取和解析操作,相应地调整流的内部状态标志。最后,它在返回之前销毁哨兵对象。
Refer : http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/
参考:http: //www.cplusplus.com/reference/istream/istream/operator%3E%3E/