function <string> std::getline (string)

(1)
istream& getline (istream& is, string& str, char delim);
(2)
istream& getline (istream& is, string& str);
Get line from stream into string
从流中获取行到字符串中

Extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, '\n', for (2)).

从is对象中提取字符并且把他们保存到str的字符串中,直到遇到定界符delim(为自己定义)(或者'\n')。


The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation.

如果到达了文件的底部或者在输入的过程中出现了错误,提取字符也会结束。(即输入结束)


If the delimiter is found, it is extracted and discarded (i.e. it is not stored and the next input operation will begin after it).

如果发现定界符,说明已经提取完毕(比如,没有存储下来并且下一个输入将会开始)


Note that any content in str before the call is replaced by the newly extracted sequence.

注意,str中的字符串会被新输入的序列所代替。


Each extracted character is appended to the string as if its member push_back was called.

如果成员函数push_back被调用了,所有字符会被追加到字符串后面


Parameters

is
istream object from which characters are extracted.
str
string object where the extracted line is stored.

The contents in the string before the call (if any) are discarded and replaced by the extracted line.

之前存放的字符串会被释放并且会被后面的新的一行输入所取代。


Return Value

The same as parameter  is.

A call to this function may set any of the internal state flags of  is if:

flagerror
eofbitThe end of the source of characters is reached during its operations.
failbitThe input obtained could not be interpreted as a valid textual representation of an object of this type.
In this case, distr preserves the parameters and internal data it had before the call.
Notice that some eofbit cases will also set failbit.
badbitAn error other than the above happened.
(see  ios_base::iostate for more info on these)

Additionally, in any of these cases, if the appropriate flag has been set with  is's member function  ios::exceptions, an exception of type ios_base::failure is thrown.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// extract to string
#include <iostream>
#include <string>

int main ()
{
  std::string name;

  std::cout << "Please, enter your full name: ";
  std::getline (std::cin,name);
  std::cout << "Hello, " << name << "!\n";

  return 0;
}


声明:由于本人水平有限翻译的不流畅还请见谅
原文地址:http://www.cplusplus.com/reference/string/string/getline/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值