std::getline(3)
β¨ NAME β¨
std::getline β std::getline (yes bestie, the name is literally the name, iconic π
)
okay so welcome to the manual page for std::getline(3), straight outta the C++ Standard Library, and itβs giving βread a whole line without losing your mindβ energy π¦
πΈ SYNOPSIS πΈ
Defined in header <string>
template< class CharT, class Traits, class Allocator >
std::basic_istream<CharT, Traits>&
getline( std::basic_istream<CharT, Traits>& input,
(1)
std::basic_string<CharT, Traits, Allocator>& str,
CharT delim );
template< class CharT, class Traits, class Allocator >
std::basic_istream<CharT, Traits>&
getline( std::basic_istream<CharT, Traits>&& input,
(2) (since C++11)
std::basic_string<CharT, Traits, Allocator>& str,
CharT delim );
template< class CharT, class Traits, class Allocator >
std::basic_istream<CharT, Traits>&
(3)
getline( std::basic_istream<CharT, Traits>& input,
std::basic_string<CharT, Traits, Allocator>& str );
template< class CharT, class Traits, class Allocator >
std::basic_istream<CharT, Traits>&
(4) (since C++11)
getline( std::basic_istream<CharT, Traits>&& input,
std::basic_string<CharT, Traits, Allocator>& str );
π«Ά the actual behavior, bestie π«Ά
getline reads characters from an input stream and places them into a string, no cap:
- For
1,2): behaves asUnformattedInputFunction, except thatinput.gcount()is not affected. After constructing and checking the sentry object, it performs the following:- Calls
str.erase(). - Extracts characters from
inputand appends them tostruntil one of the following occurs (checked in the order listed):- a) end-of-file condition on
input, in which case,getlinesetseofbit. - b) the next available input character is
delim, as tested byTraits::eq(c, delim), in which case the delimiter character is extracted frominput, but is not appended tostr. - c)
str.max_size()characters have been stored, in which casegetlinesetsfailbitand returns.
- a) end-of-file condition on
- If no characters were extracted for whatever reason (not even the discarded delimiter),
getlinesetsfailbitand returns.
- Calls
- For
3,4): same asgetline(input, str, input.widen('\n')), that is, the default delimiter is the endline character.
π PARAMETERS π
inputβ the stream to get data fromstrβ the string to put the data intodelimβ the delimiter character
π RETURN VALUE π
It returns input. Thatβs it. Thatβs the return value. slay β¨
π¦ NOTES π¦
okay so hereβs the little gotcha, bestie π When consuming whitespace-delimited input (e.g. int n; std::cin >> n;) any whitespace that follows, including a newline character, will be left on the input stream. Then when switching to line-oriented input, the first line retrieved with getline will be just that whitespace. In the likely case that this is unwanted behaviour, possible solutions include:
- An explicit extraneous initial call to
getline. - Removing consecutive whitespace with
std::cin >> std::ws. - Ignoring all leftover characters on the line of input with
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');.
π EXAMPLE π
The following example demonstrates how to use the getline function to read user input, and to process a stream line by line, or by parts of a line using the delim parameter.
// Run this code
#include <iostream>
#include <sstream>
#include <string>
int main()
{
// greet the user
std::string name;
std::cout << "What is your name? ";
std::getline(std::cin, name);
std::cout << "Hello " << name << ", nice to meet you.\n";
// read file line by line
std::istringstream input;
input.str("1\n2\n3\n4\n5\n6\n7\n");
int sum = 0;
for (std::string line; std::getline(input, line);)
sum += std::stoi(line);
std::cout << "\nThe sum is " << sum << ".\n\n";
// use separator to read parts of the line
std::istringstream input2;
input2.str("a;b;c;d");
for (std::string line; std::getline(input2, line, ';');)
std::cout << line << '\n';
}
π POSSIBLE OUTPUT π
What is your name? John Q. Public
Hello John Q. Public, nice to meet you.
The sum is 28.
a
b
c
d
π DEFECT REPORTS π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DRLWG 91, applied toC++98β behavior as published:getlinedid not behave as an unformatted input function. Correct behavior: behaves as an unformatted input function.
π SEE ALSO π
getlineextracts characters until the given character is found (public member function ofstd::basic_istream<CharT,Traits>)
source/version/date, bestie: http://cppreference.com, 2024.06.10, page std::getline(3). thatβs the whole manual, mwah π«Άπβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms