例如, 输入:
有美好的时光 也有不好的时候 结束
#include <iostream>
#include <string>
using namespace std;
//there are good times
//and there are bad times
//END
int main()
{
string str;
getline(cin, str);
cout << str;
}
一旦所有字符我怎么才能得到它,getline只得到“有美好时光”的部分
虽然这可能不是一种有效的方法,但您可以检查您在 while 循环中输入的每一行,并在它与您的结束字符串 (
"END"
) 匹配时停止,就像这样..
#include <iostream>
#include <string>
using namespace std;
//there are good times
//and there are bad times
//END
int main()
{
string str;
string currentLine;
string endLine = "END";
while (getline(cin >> ws, currentLine))
{
if (currentLine == endLine)
break;
str += currentLine + "\n";
}
cout << str;
return 0;
}