std::basic_istream<CharT,Traits>::peek
From cppreference.com
< cpp | io | basic istream
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Input/output library
Print functions (C++23)
Buffers
(C++23)
(C++98/26*)
(C++20)
Streams
Abstractions
File I/O
String I/O
Array I/O
(C++23)
(C++23)
(C++23)
(C++98/26*)
(C++98/26*)
(C++98/26*)
Synchronized Output
(C++20)
Types
Error category interface
(C++11)
(C++11)
std::basic_istream
Global objects
Member functions
(C++11)
Formatted input
Unformatted input
basic_istream::peek
Positioning
Miscellaneous
(C++11)
Member classes
Non-member functions
int_type peek();
Behaves as UnformattedInputFunction. After constructing and testing the sentry object, reads the next character from the input stream without extracting it.
[edit] Parameters
(none)
[edit] Return value
If good() == true, returns the next character as obtained by rdbuf()->sgetc().
Otherwise, returns Traits::eof().
[edit] Exceptions
failure if an error occurred (the error state flag is not goodbit ) and exceptions() is set to throw for that state.If an internal operation throws an exception, it is caught and badbit is set. If exceptions() is set for badbit
, the exception is rethrown.
[edit] Example
Run this code
#include <iostream> #include <sstream> int main() { std::istringstream s1("Hello, world."); char c1 = s1.peek(); char c2 = s1.get(); std::cout << "Peeked: " << c1 << " got: " << c2 << '\n'; }
Output:
Peeked: H got: H
[edit] See also
reads one character from the input sequence without advancing the sequence
(public member function of
(public member function of
std::basic_streambuf<CharT,Traits>
) [edit]