cplusplus.com
- TUTORIALS
- REFERENCE
- ARTICLES
- FORUM
C++
- Tutorials
- Reference
- Articles
- Forum
Reference
C library:
- <cassert> (assert.h)
- <cctype> (ctype.h)
- <cerrno> (errno.h)
- C++11 <cfenv> (fenv.h)
- <cfloat> (float.h)
- C++11 <cinttypes> (inttypes.h)
- <ciso646> (iso646.h)
- <climits> (limits.h)
- <clocale> (locale.h)
- <cmath> (math.h)
- <csetjmp> (setjmp.h)
- <csignal> (signal.h)
- <cstdarg> (stdarg.h)
- C++11 <cstdbool> (stdbool.h)
- <cstddef> (stddef.h)
- C++11 <cstdint> (stdint.h)
- <cstdio> (stdio.h)
- <cstdlib> (stdlib.h)
- <cstring> (string.h)
- C++11 <ctgmath> (tgmath.h)
- <ctime> (time.h)
- C++11 <cuchar> (uchar.h)
- <cwchar> (wchar.h)
- <cwctype> (wctype.h)
Containers:
- C++11 <array>
- <deque>
- C++11 <forward_list>
- <list>
- <map>
- <queue>
- <set>
- <stack>
- C++11 <unordered_map>
- C++11 <unordered_set>
- <vector>
Input/Output:
- <fstream>
- <iomanip>
- <ios>
- <iosfwd>
- <iostream>
- <istream>
- <ostream>
- <sstream>
- <streambuf>
Multi-threading:
- C++11 <atomic>
- C++11 <condition_variable>
- C++11 <future>
- C++11 <mutex>
- C++11 <thread>
Other:
- <algorithm>
- <bitset>
- C++11 <chrono>
- C++11 <codecvt>
- <complex>
- <exception>
- <functional>
- C++11 <initializer_list>
- <iterator>
- <limits>
- <locale>
- <memory>
- <new>
- <numeric>
- C++11 <random>
- C++11 <ratio>
- C++11 <regex>
- <stdexcept>
- <string>
- C++11 <system_error>
- C++11 <tuple>
- C++11 <type_traits>
- C++11 <typeindex>
- <typeinfo>
- <utility>
- <valarray>
<string>
class templates
classes
- string
- C++11 u16string
- C++11 u32string
- wstring
functions
- C++11 stod
- C++11 stof
- C++11 stoi
- C++11 stol
- C++11 stold
- C++11 stoll
- C++11 stoul
- C++11 stoull
- C++11 to_string
- C++11 to_wstring
string
- string::~string
- string::string
member functions
- string::append
- string::assign
- string::at
- C++11 string::back
- string::begin
- string::c_str
- string::capacity
- C++11 string::cbegin
- C++11 string::cend
- string::clear
- string::compare
- string::copy
- C++11 string::crbegin
- C++11 string::crend
- string::data
- string::empty
- string::end
- string::erase
- string::find
- string::find_first_not_of
- string::find_first_of
- string::find_last_not_of
- string::find_last_of
- C++11 string::front
- string::get_allocator
- string::insert
- string::length
- string::max_size
- string::operator[]
- string::operator+=
- string::operator=
- C++11 string::pop_back
- string::push_back
- string::rbegin
- string::rend
- string::replace
- string::reserve
- string::resize
- string::rfind
- C++11 string::shrink_to_fit
- string::size
- string::substr
- string::swap
member constants
non-member overloads
- getline (string)
- operator+ (string)
- operator<< (string)
- operator>> (string)
- relational operators (string)
- swap (string)
- Reference
- <string>
- string
- getline
function <string>
std::getline (string)
| (1) | istream& getline (istream& is, string& str, char delim); |
|---|
| (2) | istream& getline (istream& is, string& str); |
|---|
| (1) | istream& getline (istream& is, string& str, char delim);istream& getline (istream&& is, string& str, char delim); |
|---|
| (2) | istream& getline (istream& is, string& str);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)). 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. Each extracted character is appended to the string as if its member push_back was called.
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:
| flag | error |
| eofbit | The end of the source of characters is reached during its operations. |
| failbit | The 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. |
| badbit | An 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
| 1234567891011121314 | // 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; } |
Complexity
Unspecified, but generally linear in the resulting length of
str.
Iterator validity
Any iterators, pointers and references related to
str may be invalidated.
Data races
Both objects,
is and
str, are modified.
Exception safety
Basic guarantee: if an exception is thrown, both
is and
str end up in a valid state.
See also
istream::getlineGet line
(public member function)operator>> (string)Extract string from stream
(function) Home page | Privacy policy© cplusplus.com, 2000-2025 - All rights reserved -
v3.3.4s Spotted an error? contact us