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

来源:互联网 发布:同济大学复试测绘编程 编辑:程序博客网 时间:2024/05/22 05:06

当给你一行未知个数的数字时,需要读字符来处理,比较麻烦。
可以用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;}
0 0
原创粉丝点击