我试图用istringstream
一个简单的字符串分割成一系列整数使用istringstream整数:将字符串分割成C++
#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;
}
}
,我也得到:
* 1
* 2
* 3
* 3
为什么最后一个元素总是出现两次?如何解决它?
我们该如何在for()循环中做到这一点? – 2014-01-12 09:31:07
@SumitKandoi:你是什么意思?你为什么? – 2014-01-12 12:43:39
实际上,我在while()循环中试过。我想我们可以在for()循环中做到这一点 – 2014-01-12 14:32:29