Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/basic string/getline"

From cppreference.com
< cpp‎ | string‎ | basic string
Line 31: Line 31:
 
{{params}}
 
{{params}}
 
{{param list begin}}
 
{{param list begin}}
{{param list item | is | the stream to get data from}}
+
{{param list item | input | the stream to get data from}}
 
{{param list item | str | the string to put the data into}}
 
{{param list item | str | the string to put the data into}}
 
{{param list item | delim | the delimiter character}}
 
{{param list item | delim | the delimiter character}}
Line 38: Line 38:
 
{{returns}}
 
{{returns}}
 
{{tt|input}}
 
{{tt|input}}
 +
 +
{{example}}
 +
{{example cpp
 +
| The following code asks the user for their name, then greets them using that name.
 +
| code=
 +
#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.";
 +
}
 +
| output=
 +
What is your name? John Q. Public
 +
Hello John Q. Public, nice to meet you.
 +
}}

Revision as of 10:45, 2 July 2011

Template:cpp/string/sidebar Template:ddcl list begin <tr class="t-dsc-header">

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

<td></td> <td></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 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 > Template:mark c++0x feature </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 > Template:mark c++0x feature </td> </tr> Template:ddcl list end

Reads in unformatted data from a stream into a string. Stops once a character equal to the delimiter is found, or the stream is exhausted. The first version uses delim as the delimiter, the second version uses Template:cpp as the delimiter. The delimiter character is discarded from the stream and not placed in the string.

Template:params

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

Template:returns input

Template:example cpp