std::basic_istream::unget(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::basic_istream::unget(3) πŸ’…

okay so bestie, this page is straight out of the C++ Standard Libary manual, and it's all about std::basic_istream::unget. it's giving "put that one character back" energy, and honestly? obsessed. ✨

🌸 NAME 🌸

  • std::basic_istream::unget β€” std::basic_istream::unget (yes, the manual says it twice, iconic πŸ’…)

✨ Synopsis ✨

basic_istream& unget();

it makes the most recently extracted character available again. literally one-character undo, no cap. 🫢

First, it clears eofbit. Then, since C++11, unget behaves as UnformattedInputFunction. After constructing and checking the sentry object, if any ios_base::iostate flags are set, the function sets failbit and returns. Otherwise, it calls rdbuf()->sungetc().

If rdbuf()->sungetc() returns Traits::eof(), it calls setstate(badbit).

In any case, it sets the gcount() counter to zero. periodt. πŸ’…

πŸŽ€ Parameters πŸŽ€

  • (none) β€” bestie, it takes no arguments. zero. nada. πŸ“

πŸ’– Return value πŸ’–

  • *this β€” yup, it returns the stream itself, main character energy. πŸ‘‘

πŸ”₯ 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.

🧚 Example 🧚

run this code, bestie:

// Run this code

#include <iostream>
#include <sstream>

int main()
{
    std::istringstream s1("Hello, world.");
    char c1 = s1.get();
    if (s1.unget())
    {
        char c2 = s1.get();
        std::cout << "Got: '" << c1 << "'. Got again: '" << c2 <<
       "'.\n";
    }
}

🌈 Output 🌈

Got: 'H'. Got again: 'H'.

πŸ‘‘ See also πŸ‘‘

  • sungetc β€” moves the next pointer in the input sequence back by one (public member function of std::basic_streambuf<CharT,Traits>)
  • get β€” extracts characters (public member function)
  • peek β€” reads the next character without extracting it (public member function)
  • putback β€” puts a character into input stream (public member function)

kisses and slay, bestie πŸ’‹βœ¨ β€” source: http://cppreference.com; version/date: 2024.06.10; page: std::basic_istream::unget(3). πŸ«ΆπŸ’…

πŸ“¦ where this page comes from