Namespaces
Variants
Views
Actions

std::getline

From cppreference.com
< cpp‎ | string‎ | basic string
Revision as of 10:38, 2 November 2012 by P12bot (Talk | contribs)

 
 
 
std::basic_string
 

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <string>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td >
template< class CharT, class Traits, class Allocator >

std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>& input,
                                           std::basic_string<CharT,Traits,Allocator>& str,

                                           CharT delim );
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class CharT, class Traits, class Allocator >

std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>&& input,
                                           std::basic_string<CharT,Traits,Allocator>& str,

                                           CharT delim );
</td>

<td > (1) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
template< class CharT, class Traits, class Allocator >

std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>& input,

                                           std::basic_string<CharT,Traits,Allocator>& str );
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class CharT, class Traits, class Allocator >

std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>&& input,

                                           std::basic_string<CharT,Traits,Allocator>& str );
</td>

<td > (2) </td> <td > (since C++11) </td> </tr> Template:ddcl list end

getline reads characters from an input stream and places them into a string:

1) Behaves as UnformattedInputFunction, except that input.gcount() is not affected. After constructing and checking the sentry object, performs the following:
1) Calls str.erase()
2) Extracts characters from input and appends them to str until one of the following occurs (checked in the order listed)
a) end-of-file condition on input, in which case, getline sets eofbit and returns.
b) the next available input character is delim, as tested by Traits::eq(c, delim), in which case the delimiter character is extracted from input, but is not appended to str.
c) str.max_size() characters have been stored, in which case getline sets failbit and returns.
3) If no characters were extracted for whatever reason (not even the discarded delimiter), getline sets failbit and returns.
2) Same as getline(input, str, input.widen(’\n’)), that is, the default delimiter is the endline character.

Parameters

input - the stream to get data from
str - the string to put the data into
delim - the delimiter character

Return value

input

Example

The following code asks the user for their name, then greets them using that name.

#include <string>
#include <iostream>
 
int main()
{
    std::string name;
    std::cout << "What is your name? ";
    std::getline(std::cin, name);
    std::cout << "Hello " << name << ", nice to meet you.";
}

Possible output:

What is your name? John Q. Public
Hello John Q. Public, nice to meet you.