C++读取一串不知个数的数字

1 #include <iostream> 
 2 
 3 using namespace std; 
 4 
 5 int main(){
 6     int shuzu[100];
 7     int i = 0;
 8     while(cin>>shuzu[i]){
 9         i++;
10     }
11     for(int j = 0; j < i; j++){
12         cout<<shuzu[j]<<" ";
13     }
14     cout<<endl;
15     cout<<i<<endl;
16 
17     return 0;
18 }

输入:1 2 3 4 5

输出:1 2 3 4 5

5

输入:1 2 3 4 5

6 7

输出:1 2 3 4 5 6 7

7

需要输入EOF表示输入结束,如下:

windows下, 请按 ctrl+z;

Unix/Linux下, 请按 Ctrl+D.

关于cin的返回值可以参考:http://blog.csdn.net/kongbai308416350/article/details/4470914
原文链接: https://www.cnblogs.com/hu983/p/5321596.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/230857

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午2:48
下一篇 2023年2月13日 下午2:49

相关推荐