【发布时间】:2011-12-15 07:44:04
【问题描述】:
我是 C++ 编程的新手。我已经阅读了如何使用向量(Int tokenizer)在 SO 问题中进行解析。但是我已经尝试了以下数组。我只能从字符串中解析一个数字。如果输入字符串是“11 22 33 等”。
#include<iostream>
#include<iterator>
#include<vector>
#include<sstream>
using namespace std;
int main()
{
int i=0;
string s;
cout<<"enter the string of numbers \n";
cin>>s;
stringstream ss(s);
int j;
int a[10];
while(ss>>j)
{
a[i]=j;
i++;
}
for(int k=0;k<10;k++)
{
cout<<"\t"<<a[k]<<endl;
}
}
如果我输入“11 22 33”
output
11
and some garbage values.
如果我已经初始化 stringstream ss("11 22 33"); 那么它工作正常。我做错了什么?
【问题讨论】:
标签: c++ stringstream