语言:c++
头文件:#include <string>
函数原型:
istream& getline (istream& is, string& str);
注:is istream对象,从中提取字符。
str 字符串对象,其中存储提取的行。
调用之前的字符串中的内容(如果有)被丢弃并被提取的行替换。
注意:
使用getline()函数的好处在于当你输入的字符串里有空格时候,而且空格不能涉略,使用这个函数可以很好的解决这个问题
例如:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name,test[3];
cout << "Please, enter your full name: ";
getline(cin, name);
cout << "Hello, " << name << "!\n";
for (int i = 0; i < 3; i++)
{
getline(cin, test[i]);
}
for (int i = 0; i < 3; i++)
{
cout << test[i] << endl;
}
system("pause");
}
运行结果: