std::basic_istream::ignore(3)

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

πŸ’… std::basic_istream::ignore πŸ’…

okay bestie, welcome to the std::basic_istream::ignore(3) page from the C++ Standard Libary manual πŸ’– this one is literally about yeeting characters out of an input stream, and ngl it's kind of iconic.


🌸 NAME 🌸

std::basic_istream::ignore β€” yep, it's std::basic_istream::ignore again, main character energy πŸ’…


πŸ“œ Synopsis πŸ“œ

Here's the signature, bestie β€” copy it exactly, no cap:

basic_istream& ignore( std::streamsize count = 1, int_type delim =
Traits::eof()
);

So what does it do? It extracts and discards characters from the input stream until and including delim. Bye-bye, unwanted input 🫢

ignore behaves as an UnformattedInputFunction. After constructing and checking the sentry object, it extracts characters from the stream and discards them until any of these conditions happens:

  • count characters were extracted. This test is disabled in the special case when count equals std::numeric_limits<std::streamsize>::max().
  • an end-of-file condition occurs in the input sequence, in which case the function calls setstate(eofbit).
  • the next available character c in the input sequence is delim, as determined by Traits::eq_int_type(Traits::to_int_type(c), delim). The delimiter character is extracted and discarded. This test is disabled if delim is Traits::eof().

πŸŽ€ Parameters πŸŽ€

  • count β€” number of characters to extract.
  • delim β€” delimiting character to stop the extraction at. It is also extracted.

✨ Return value ✨

  • *this

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

The following example uses ignore to skip over non-numeric input:

// Run this code

#include <iostream>
#include <limits>
#include <sstream>

constexpr auto max_size = std::numeric_limits<std::streamsize>::max();

int main()
{
    std::istringstream input("1\n"
                             "some non-numeric input\n"
                             "2\n");
    for (;;)
    {
        int n;
        input >> n;

        if (input.eof() || input.bad())
            break;
        else if (input.fail())
        {
            input.clear(); // unset failbit
            input.ignore(max_size, '\n'); // skip bad input
        }
        else
            std::cout << n << '\n';
    }
}

Output:

1
2

πŸ’€ Defect reports πŸ’€

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • DR LWG 172 β€” applied to C++98: the type of count was misspecified as int; the corrected behavior is std::streamsize.

πŸ‘‘ See also πŸ‘‘

  • get β€” extracts characters (public member function)
  • getline β€” extracts characters until the given character is found (public member function)

mwah πŸ’‹ this page came from http://cppreference.com, dated 2024.06.10, for std::basic_istream::ignore(3) β€” k bye bestie, go ignore some bad input 🫢✨

πŸ“¦ where this page comes from