Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |io‎ |basic istream
       
       
       
       
      basic_istream& ignore(std::streamsize count=1, int_type delim= Traits::eof());

      Extracts and discards characters from the input stream until and includingdelim.

      ignore behaves as anUnformattedInputFunction. After constructing and checking the sentry object, it extracts characters from the stream and discards them until any of the following conditions occurs:

      • end of file conditions occurs in the input sequence, in which case the function callssetstate(eofbit).
      • the next available characterc in the input sequence isdelim, as determined byTraits::eq_int_type(Traits::to_int_type(c), delim). The delimiter character is extracted and discarded. This test is disabled ifdelim isTraits::eof().

      Contents

      [edit]Parameters

      count - number of characters to extract
      delim - delimiting character to stop the extraction at. It is also extracted

      [edit]Return value

      *this

      [edit]Exceptions

      [edit]
      failure if an error occurred (the error state flag is notgoodbit) andexceptions() is set to throw for that state.

      If an internal operation throws an exception, it is caught andbadbit is set. Ifexceptions() is set forbadbit, the exception is rethrown.

      [edit]Example

      The following example usesignore to skip over non-numeric input:

      Run this code
      #include <iostream>#include <limits>#include <sstream> constexprauto 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;elseif(input.fail()){            input.clear();// unset failbit            input.ignore(max_size,'\n');// skip bad input}elsestd::cout<< n<<'\n';}}

      Output:

      12

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 172C++98the type ofcount was misspecified asintcorrected tostd::streamsize

      [edit]See also

      extracts characters
      (public member function)[edit]
      extracts characters until the given character is found
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_istream/ignore&oldid=158517"

      [8]ページ先頭

      ©2009-2025 Movatter.jp