std::basic_istream::peek(3)

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

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

okay bestie, this page is from the C++ Standard Libary (yes, spelled like that in the manual β€” iconic πŸ’€). It’s all about std::basic_istream::peek, and it’s lowkey one of the cutest little stream functions.


🌸 NAME 🌸

std::basic_istream::peek β€” std::basic_istream::peek

✨ Synopsis ✨

int_type peek();

Behaves as UnformattedInputFunction. After constructing and testing the sentry object, reads the next character from the input stream without extracting it. peek only, no take β€” look but don’t touch energy πŸ‘€βœ¨.

πŸŽ€ Parameters πŸŽ€

  • (none) β€” bestie, there are literally no parameters here. πŸ’…

πŸ’– Return value πŸ’–

If good() == true, returns the next character as obtained by rdbuf()->sgetc().

Otherwise, returns Traits::eof().

🚨 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, exceptions can happen β€” this is the part where you check your exception settings, fr fr.

πŸ“ 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

πŸ‘‘ See also πŸ‘‘

  • sgetc β€” reads one character from the input sequence without advancing the sequence (public member function of std::basic_streambuf<CharT,Traits>)
  • get β€” extracts characters (public member function)
  • unget β€” unextracts a character (public member function)

mwah πŸ’‹ β€” sourced from http://cppreference.com, version 2024.06.10, and this iconic page is std::basic_istream::peek(3). bye bestie πŸ«ΆπŸ’–

πŸ“¦ where this page comes from