std::basic_...f::sputbackc(3)
π std::basic_...f::sputbackc(3) π
okay bestie, welcome to the C++ Standard Libary manual page for std::basic_...f::sputbackc(3) β yes, that title is literally abbreviated with ..., and the function we're actually talking about is std::basic_streambuf::sputbackc. No cap, this one is a little unget-flavored icon. π
β¨ NAME β¨
std::basic_streambuf::sputbackc β std::basic_streambuf::sputbackc.
πΈ Synopsis πΈ
int_type sputbackc( char_type c );
okay so sputbackc puts a character back to the get area. π«Ά
If a putback position is available in the get area β that's gptr() > eback() β and the character c is equal to the character one position to the left of gptr() (as determined by Traits::eq(c, gptr()[-1])), then it simply decrements the next pointer, gptr(). Easy slay.
Otherwise, it calls pbackfail(Traits::to_int_type(c)) to either back up the get area or to modify both the get area and possibly the associated character sequence. That part is giving "the plot thickens" energy. π
The I/O stream function basic_istream::putback is implemented in terms of this function. iykyk.
π Parameters π
cβ character to put back. That's the whole parameter, bestie.
π Return value π
If putback position was available, it returns the character that the next pointer is now pointing at, converted to int_type with Traits::to_int_type(*gptr()). The next single-character input from this streambuf will return this character. Cute.
If putback position was not available, it returns what pbackfail() returns, which is Traits::eof() on failure. Sad but true. π₯
π¦ Example π¦
// Run this code
#include <iostream>
#include <sstream>
int main()
{
std::stringstream s("abcdef"); // gptr() points to 'a' in "abcdef"
std::cout << "Before putback, string holds " << s.str() << '\n';
char c1 = s.get(); // c1 = 'a', gptr() now points to 'b' in
"abcdef"
char c2 = s.rdbuf()->sputbackc('z'); // same as s.putback('z')
// gptr() now points to 'z' in
"zbcdef"
std::cout << "After putback, string holds " << s.str() << '\n';
char c3 = s.get(); // c3 = 'z', gptr() now points to 'b' in
"zbcdef"
char c4 = s.get(); // c4 = 'b', gptr() now points to 'c' in
"zbcdef"
std::cout << c1 << c2 << c3 << c4 << '\n';
s.rdbuf()->sputbackc('b'); // gptr() now points to 'b' in "zbcdef"
s.rdbuf()->sputbackc('z'); // gptr() now points to 'z' in "zbcdef"
int eof = s.rdbuf()->sputbackc('x'); // nothing to unget:
pbackfail() fails
if (eof == EOF)
std::cout << "No room to putback after 'z'\n";
}
π Output: π
Before putback, string holds abcdef
After putback, string holds zbcdef
azzb
No room to putback after 'z'
π See also π
sungetcβ moves the next pointer in the input sequence back by one (public member function).putbackβ puts a character into input stream (public member function ofstd::basic_istream<CharT,Traits>).
okay that's the whole page, bestie π source/version: http://cppreference.com Β· date: 2024.06.10 Β· footer title: std::basic_...f::sputbackc(3). 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