getline()函数是C++库函数。它会生成一个包含一串从输入流读入的字符的字符串,直到以下情况发生会导致生成的此字符串结束。1)到文件结束,2)遇到函数的定界符,3)输入达到最大限度。
我们一般用string输入字符串时不能读取空格,我们可以用getline() 函数从输入流中读取,getline()函数之前的回车动作也会读入,并且作为结束符,这时要用getchar()来吸收多余的回车。
#include <bits/stdc++.h>
using namespace std;
string str;
int main()
{
int t; cin >> t;
getchar();
while(t--)
{
getline(cin,str);
cout << str << endl;
}
}