From cppreference.com
basic_istream&unget();Makes the most recently extracted character available again.
First clears
, then behaves as
. After constructing and checking the sentry object, if any ios_base::iostate flags are set, the function sets failbit and returns. Otherwise, calls rdbuf()->sungetc().
If rdbuf()->sungetc() returns Traits::eof(), calls setstate(badbit).
In any case, sets the gcount() counter to zero.
Parameters
(none)
Return value
*this
Exceptions
if an error occurred (the error state flag is not
) and
is set to throw for that state.
If an internal operation throws an exception, it is caught and
is set. If
is set for badbit, the exception is rethrown.
Example
Run this code
#include<iostream>#include<sstream>intmain(){std::istringstreams1("Hello, world.");charc1=s1.get();if(s1.unget()){charc2=s1.get();std::cout<<"Got: '"<<c1<<"'. Got again: '"<<c2<<"'.\n";}}Output:
Got: 'H'. Got again: 'H'. Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
(
) C++98 unget doesn't clear eofbitclears eofbit before doing anything else See also
moves the next pointer in the input sequence back by one
(public member function of std::basic_streambuf<CharT,Traits>)
extracts characters
(public member function)
reads the next character without extracting it
(public member function)
puts a character into input stream
(public member function)