您的位置:首页 > 编程语言 > C语言/C++

读入一行由空格隔开的数字

2015-06-15 21:31 337 查看
当给你一行未知个数的数字时,需要读字符来处理,比较麻烦。

可以用C++封装的stringstream来处理的。

#include <stdio.h>
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
int a[1005], cnt;
string s;
while(getline(cin, s))
{
cnt = 0;
stringstream ss(s);
int x;
while(ss >> x)
{
a[cnt++] = x;
}
for(int i = 0; i < cnt; i++)
{
printf("%d ", a[i]);
}
puts("");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++