Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::istreambuf_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 CharT,class Traits=std::char_traits<CharT>>

      class istreambuf_iterator
         :publicstd::iterator<std::input_iterator_tag,
                                 CharT,typename Traits::off_type,

                                 /* unspecified */, CharT>
      (until C++17)
      template<class CharT,class Traits=std::char_traits<CharT>>
      class istreambuf_iterator;
      (since C++17)

      std::istreambuf_iterator is a single-pass input iterator that reads successive characters from thestd::basic_streambuf object for which it was constructed.

      The default-constructedstd::istreambuf_iterator is known as theend-of-stream iterator. When astd::istreambuf_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.

      std::istreambuf_iterator has a trivial copy constructor, a constexpr default constructor, and a trivial destructor.

      (since C++11)

      Contents

      [edit]Member types

      Member type Definition
      iterator_categorystd::input_iterator_tag
      value_typeCharT
      difference_typetypename Traits::off_type
      pointer/* unspecified */
      referenceCharT
      char_typeCharT
      traits_typeTraits
      int_typetypename Traits::int_type
      streambuf_typestd::basic_streambuf<CharT, Traits>
      istream_typestd::basic_istream<CharT, Traits>
      /* proxy */ Implementation-defined class type.
      Aproxy object holds achar_type character and astreambuf_type* pointer.
      Dereferencing aproxy object withoperator* yields the stored character.
      (exposition-only member type*)

      Member typesiterator_category,value_type,difference_type,pointer andreference are required to be obtained by inheriting fromstd::iterator<std::input_iterator_tag, CharT,typename Traits::off_type,/* unspecified */, CharT>.

      (until C++17)

      The member typepointer is usuallyCharT* (seebelow).

      [edit]Member functions

      constructs a newistreambuf_iterator
      (public member function)[edit]
      (destructor)
      (implicitly declared)
      destructs anistreambuf_iterator
      (public member function)[edit]
      obtains a copy of the current character
      (public member function)[edit]
      advances the iterator
      (public member function)[edit]
      tests if bothistreambuf_iterators are end-of-stream or if both are valid
      (public member function)[edit]

      [edit]Non-member functions

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

      [edit]Notes

      The resolution ofLWG issue 659 introducedoperator->. It is expected that given anstd::istreambuf_iteratori, the expressions(*i).m andi->m have the same effect.

      However, the resolution does not provide a formal specification of its behavior. Thus it is implemented differently, including returningnullptr, returning the address of a temporary, or does even provide the member at all. Its intended behavior can hardly be achieved, and it is removed by the resolution ofLWG issue 2790.

      The resolution ofLWG issue 659 also made the member typepointer unspecified in order to allowoperator-> to return a proxy. This is to allowoperator-> to compile whenCharT is not a class type.

      [edit]Example

      Run this code
      #include <iostream>#include <iterator>#include <sstream>#include <string> int main(){// typical use case: an input stream represented as a pair of iteratorsstd::istringstream in{"Hello, world"};    std::istreambuf_iterator<char> it{in}, end;std::string ss{it, end};std::cout<<"ss has "<< ss.size()<<" bytes; ""it holds\""<< ss<<"\"\n"; // demonstration of the single-pass naturestd::istringstream s{"abc"};    std::istreambuf_iterator<char> i1{s}, i2{s};std::cout<<"i1 returns '"<<*i1<<"'\n""i2 returns '"<<*i2<<"'\n"; ++i1;std::cout<<"after incrementing i1, but not i2:\n""i1 returns '"<<*i1<<"'\n""i2 returns '"<<*i2<<"'\n"; ++i2;std::cout<<"after incrementing i2, but not i1:\n""i1 returns '"<<*i1<<"'\n""i2 returns '"<<*i2<<"'\n";}

      Output:

      ss has 12 bytes; it holds "Hello, world"i1 returns 'a'i2 returns 'a'after incrementing i1, but not i2:i1 returns 'b'i2 returns 'b'after incrementing i2, but not i1:i1 returns 'c'i2 returns 'c'

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 659C++981.std::istreambuf_iterator did not haveoperator->
      2. the member typepointer was specified asCharT*
      1. added
      2. made unspecified
      LWG 2790C++98theoperator-> added byLWG issue 659 was not usefulremoved

      [edit]See also

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

      [8]ページ先頭

      ©2009-2025 Movatter.jp