要求在一行输入多个数据
方法一
#include<sstream>
string st;
getline(cin, st);
stringstream qwe(st);
int a[3]; int s = 0;
while (qwe)
qwe >> a[s++];
注解:
getline(cin,string)是从cin中把一行赋给string
stringstream qwe(st); 是指把st串中的内容赋给qwe流,正好把其中的 ’ ’ 变成数字的间隔
顺带一提
这是数组用的getline
char a[]; cin.getline(a,size)
还有string a 转char[] b是
strcpy(b, a.c_str());
方法二
while (getchar() != '\n')
cin >> a[i++];
通过读回车来判定
没有结尾标志的输入
while (scanf("%d", &a) != EOF)
cout << a << endl;
还有一种cin版的
while (cin >> a)
cout << a << endl;
不过都仅能读一行