Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::istream_iterator

      From cppreference.com
      <cpp‎ |iterator
       
       
      Iterator library
      Iterator concepts
      Iterator primitives
      Algorithm concepts and utilities
      Indirect callable concepts
      Common algorithm requirements
      (C++20)
      (C++20)
      (C++20)
      Utilities
      (C++20)
      Iterator adaptors
      Range access
      (C++11)(C++14)
      (C++14)(C++14)  
      (C++11)(C++14)
      (C++14)(C++14)  
      (C++17)(C++20)
      (C++17)
      (C++17)
       
       
      Defined in header<iterator>
      template<class T,

               class CharT=char,
               class Traits=std::char_traits<CharT>,
               class Distance=std::ptrdiff_t>
      class istream_iterator

         :publicstd::iterator<std::input_iterator_tag, T, Distance,const T*,const T&>
      (until C++17)
      template<class T,

               class CharT=char,
               class Traits=std::char_traits<CharT>,
               class Distance=std::ptrdiff_t>

      class istream_iterator;
      (since C++17)

      std::istream_iterator is a single-pass input iterator that reads successive objects of typeT from thestd::basic_istream object for which it was constructed, by calling the appropriateoperator>>. The actual read operation is performed when the iterator is incremented, not when it is dereferenced. The first object is read when the iterator is constructed. Dereferencing only returns a copy of the most recently read object.

      The default-constructedstd::istream_iterator is known as theend-of-stream iterator. When a validstd::istream_iterator reaches the end of the underlying stream, it becomes equal to the end-of-stream iterator. Dereferencing or incrementing it further invokes undefined behavior. An end-of-stream iterator remains in the end-of-stream state even if the underlying stream changes state. Absent a reassignment, it cannot become a non-end-of-stream iterator anymore.

      A typical implementation ofstd::istream_iterator holds two data members: a pointer to the associatedstd::basic_istream object and the most recently read value of typeT.

      T must meet theDefaultConstructible,CopyConstructible, andCopyAssignable requirements.

      Contents

      [edit]Member types

      Member type Definition
      iterator_categorystd::input_iterator_tag
      value_typeT
      difference_typeDistance
      pointerconst T*
      referenceconst T&
      char_typeCharT
      traits_typeTraits
      istream_typestd::basic_istream<CharT, Traits>

      Member typesiterator_category,value_type,difference_type,pointer andreference are required to be obtained by inheriting fromstd::iterator<std::input_iterator_tag, T, Distance,const T*,const T&>.

      (until C++17)

      [edit]Member functions

      constructs a newistream_iterator
      (public member function)[edit]
      destructs anistream_iterator, including the cached value
      (public member function)[edit]
      returns the current element
      (public member function)[edit]
      advances the iterator
      (public member function)[edit]

      [edit]Non-member functions

      (removed in C++20)
      compares twoistream_iterators
      (function template)[edit]

      [edit]Notes

      When reading characters,std::istream_iterator skips whitespace by default (unless disabled withstd::noskipws or equivalent), whilestd::istreambuf_iterator does not. In addition,std::istreambuf_iterator is more efficient, since it avoids the overhead of constructing and destructing the sentry object once per character.

      [edit]Example

      Run this code
      #include <algorithm>#include <iostream>#include <iterator>#include <numeric>#include <sstream> int main(){std::istringstream str("0.1 0.2 0.3 0.4");std::partial_sum(std::istream_iterator<double>(str),                     std::istream_iterator<double>(),std::ostream_iterator<double>(std::cout," ")); std::istringstream str2("1 3 5 7 8 9 10");auto it=std::find_if(std::istream_iterator<int>(str2),                           std::istream_iterator<int>(),[](int i){return i%2==0;}); if(it!= std::istream_iterator<int>())std::cout<<"\nThe first even number is "<<*it<<".\n";//" 9 10" left in the stream}

      Output:

      0.1 0.3 0.6 1 The first even number is 8.

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      P0738R2C++98the first read might be deferred to the first dereferencealways performed in the constructor

      [edit]See also

      output iterator that writes tostd::basic_ostream
      (class template)[edit]
      input iterator that reads fromstd::basic_streambuf
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/iterator/istream_iterator&oldid=159916"

      [8]ページ先頭

      ©2009-2025 Movatter.jp