string::getline

說明如何使用 string::getline Visual C++ 標準樣板程式庫 (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 函式會建立字串,包含所有輸入資料流中的字元,直到發生下列情況其中一項:-檔案結尾。位遇到的分隔符號。- is. 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;;
}
  請測試這個部分
  測試這
輸入一個句子 (使用 <space> 做為分隔符號): 對此進行測試
您輸入: 測試

需求

標頭: <string>

請參閱

概念

標準樣板程式庫範例