Namespaces
Variants

std::basic_istream<CharT,Traits>:: ignore

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

入力ストリームから文字を抽出して破棄し、 delim を含むまで処理します。

ignore UnformattedInputFunction として動作します。セントリオブジェクトを構築・チェックした後、以下のいずれかの条件が発生するまで、ストリームから文字を抽出して破棄します:

  • 入力シーケンスでファイル終端条件が発生した場合、関数は setstate ( eofbit ) を呼び出します。
  • 入力シーケンスにおける次の利用可能な文字 c delim と等しい場合( Traits :: eq_int_type ( Traits :: to_int_type ( c ) , delim ) によって判定)。区切り文字は抽出され破棄されます。この検査は delim Traits :: eof ( ) の場合には無効になります。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - C++関連の専門用語(Parameters、Return value、Exceptions、Example、Defect reports、See also)は原文のまま保持しました - HTMLタグ、属性、class名、id名は一切変更していません - 数値や書式設定は完全に保持されています

パラメータ

count - 抽出する文字数
delim - 抽出を停止する区切り文字。これも抽出される

戻り値

* this

例外

failure **翻訳結果:** failure **説明:** - HTMLタグ(` `, ` `)と属性(`class`, `href`, `title`)は翻訳せず保持 - `failure`はC++の標準ライブラリクラス名であるため、専門用語として翻訳せず保持 - 元のフォーマットと構造を完全に維持 if an error occurred (the error state flag is not goodbit ) and exceptions() is set to throw for that state.

内部操作が例外をスローした場合、それは捕捉され、 badbit が設定されます。 exceptions() badbit に対して設定されている場合、例外は再スローされます。

以下の例では、数値以外の入力をスキップするために ignore を使用しています:

#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';
    }
}

出力:

1
2

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 172 C++98 count の型が誤って int と指定されていた std::streamsize に修正

関連項目

文字を抽出する
(公開メンバ関数)
指定された文字が見つかるまで文字を抽出する
(公開メンバ関数)