首页IT科技getline(cin,s)函数用法(getline C++ Reference)

getline(cin,s)函数用法(getline C++ Reference)

时间2025-06-21 05:06:44分类IT科技浏览5141
导读:std::getline...

std::getline

istream& getline ( istream& is, string& str, char delim ); istream& getline ( istream& is, string& str );
Get line from stream

Extracts characters from is and stores them into str until a delimitation character is found.

The delimiter character is delim for the first function version, and \n (newline character) for the second. 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.

Notice that unlike the c-string versions of istream::getline, these string versions are implemented as global functions instead of members of the stream class.

Parameters

is istream object on which the extraction operation is performed. str string object where the extracted content is stored. delim The delimiting character. The operation of extracting successive characters is stopped when this character is read.

Return Value

The same as parameter is.

Errors are signaled by modifying the internal state flags:

flag error eofbit The end of the source of characters is reached during its operations. failbit No characters were extracted because the end was prematurely found.Notice that some eofbit cases will also set failbit. badbit An error other than the above happened.

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

Example

1234567891011 // getline with strings #include <iostream> #include <string> using namespace std; int main () { string str; cout << "Please enter full name: "; getline (cin,str); cout << "Thank you, " << str << ".\n"; }
声明:本站所有文章             ,如无特殊说明或标注                   ,均为本站原创发布            。任何个人或组织       ,在未征得本站同意时      ,禁止复制            、盗用                    、采集       、发布本站内容到任何网站            、书籍等各类媒体平台                    。如若本站内容侵犯了原著者的合法权益                   ,可联系我们进行处理       。

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
vue事件对象的使用(vue中的事件触发(emit)及监听(on)问题)