string::getline

在 Visual C++ 演示如何使用 字符串:: getline 标准模板库 (STL)类。

template<class _E, class _TYPE, class _A> inline
   basic_istream<_E, _TYPE>& getline(
   basic_istream<_E, _TYPE>& Istream,
   basic_string<_E, _TYPE, _A>& Xstring,
   const _E _D=_TYPE::newline( )
   );

备注

说明说明

类/参数名在原型不匹配版本在头文件。修改某些提高可读性。

getline 函数创建包含所有输入流中的字符的字符串,直到下列情况之一:- 文件结尾。- 这个分隔符遇到。- max_str 元素中提取的。

示例

// string_getline_sample.cpp
// compile with: /EHsc
// Illustrates how to use the getline function to read a
// line of text from the keyboard.
//
// Functions:
//
//    getline       Returns a string from the input stream.
//////////////////////////////////////////////////////////////////////

#pragma warning(disable:4786)
#include <string>
#include <iostream>

using namespace std ;

int main()
{
   string s1;
   cout << "Enter a sentence (use <space> as the delimiter): ";
   getline(cin,s1, ' ');
   cout << "You entered: " << s1 << endl;;
}
  测试 thistest
   thisEnter
 句子 (使用空格作为分隔符):测试此
您输入的内容:测试

要求

标题: string

请参见

概念

标准模板库示例