std::basic_...eam::putback(3)
okay so bestie, welcome to the C++ Standard Libary manual π β this is the std::basic_...eam::putback(3) page, and it's all about the std::basic_istream::putback member function. no cap, it's giving βput a character backβ energy. π«Ά
π NAME π
std::basic_istream::putback β literally std::basic_istream::putback; that's the name, bestie. π
π Synopsis π
Here's the signature, copy it exactly:
basic_istream& putback( char_type ch );
Okay so what it does: puts the character ch back to the input stream so the next extracted character will be ch. β¨
First clears eofbit, then behaves as UnformattedInputFunction. After constructing and checking the sentry object, if rdbuf() is not null, calls rdbuf()->sputbackc(ch), which calls rdbuf()->pbackfail(ch) if ch does not equal the most recently extracted character.
If rdbuf() is null or if rdbuf->sputbackc(ch) returns Traits::eof(), calls setstate(badbit).
In any case, sets the gcount() counter to zero. periodt. π
π Parameters π
chβ character to put into input stream.
π«Ά Return value π«Ά
It returns *this. that's it, that's the return value, slay. β¨
π 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.
β οΈ bestie, this is the βthings went bad and exceptions are configured to yellβ zone. read the flags, okay? π«‘
π» Example π»
Demonstrates the difference between modifying and non-modifying putback(). π¦
// Run this code
#include <iostream>
#include <sstream>
int main()
{
std::stringstream s1("Hello, world"); // IO stream
s1.get();
if (s1.putback('Y')) // modifies the buffer
std::cout << s1.rdbuf() << '\n';
else
std::cout << "putback failed\n";
std::cout << "--\n";
std::istringstream s2("Hello, world"); // input-only stream
s2.get();
if (s2.putback('Y')) // cannot modify input-only buffer
std::cout << s2.rdbuf() << '\n';
else
std::cout << "putback failed\n";
s2.clear();
std::cout << "--\n";
if (s2.putback('H')) // non-modifying putback
std::cout << s2.rdbuf() << '\n';
else
std::cout << "putback failed\n";
}
π€ Output π€
Yello, world
--
putback failed
--
Hello, world
π See also π
sputbackcβ puts one character back in the input sequence (public member function ofstd::basic_streambuf<CharT,Traits>).ungetβ unextracts a character (public member function).peekβ reads the next character without extracting it (public member function).
okay bye bestie π source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::basic_...eam::putback(3) π
β¨
π¦ 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