getline(input,str,delim)
Parameters
input | - | the stream to get data from |
str | - | the string to put the data into |
delim | - | the delimiter character |
Return value
input
注:
getline是在string头文件中定义的一个函数,以一个istream对象和一个string对象作为输入参数。该函数首先读取输入流的内容直到遇到换行符停止,然后将读入的数据存入string对象,最后返回istream对象。其中换行符读入但是不保留。即换行符'\n'作为默认delim的参数值。
详细可查:http://en.cppreference.com/w/cpp/string/basic_string/getline
代码示例:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string buf;
cout<<"Type your input:\n";
getline(cin,buf);
cout<<buf;
return 0;
}
测试:
Type your input:
Hello,World! --By Dennis.Ritchie
Hello,World! --By Dennis.Ritchie
#include <string>
#include <iostream>
using namespace std;
int main()
{
string buf;
cout<<"Type your input:\n";
getline(cin,buf,'.');
cout<<buf;
return 0;
}
测试:
Type your input:
Hello,World! --By Dennis.Ritchie
Hello,World! --By Dennis