Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ostream_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 ostream_iterator

         :publicstd::iterator<std::output_iterator_tag,void,void,void,void>
      (until C++17)
      template<class T,

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

      class ostream_iterator;
      (since C++17)

      std::ostream_iterator is a single-passLegacyOutputIterator that writes successive objects of typeT into thestd::basic_ostream object for which it was constructed, usingoperator<<. Optional delimiter string is written to the output stream after every write operation. The write operation is performed when the iterator (whether dereferenced or not) is assigned to. Incrementing thestd::ostream_iterator is a no-op.

      In a typical implementation, the only data members ofstd::ostream_iterator are a pointer to the associatedstd::basic_ostream and a pointer to the first character in the delimiter string.

      When writing characters,std::ostreambuf_iterator is more efficient, since it avoids the overhead of constructing and destructing the sentry object once per character.

      Contents

      [edit]Member types

      Member type Definition
      iterator_categorystd::output_iterator_tag
      value_typevoid
      difference_type

      void

      (until C++20)

      std::ptrdiff_t

      (since C++20)
      pointervoid
      referencevoid
      char_typeCharT
      traits_typeTraits
      ostream_typestd::basic_ostream<CharT, Traits>

      Member typesiterator_category,value_type,difference_type,pointer andreference are required to be obtained by inheriting fromstd::iterator<std::output_iterator_tag,void,void,void,void>.

      (until C++17)

      [edit]Member functions

      constructs a new ostream_iterator
      (public member function)[edit]
      destructs anostream_iterator
      (public member function)[edit]
      writes an object to the associated output sequence
      (public member function)[edit]
      no-op
      (public member function)[edit]
      no-op
      (public member function)[edit]

      [edit]Example

      Run this code
      #include <iostream>#include <iterator>#include <numeric>#include <sstream> int main(){std::istringstream str("0.11 0.22 0.33 0.44"); std::partial_sum(std::istream_iterator<double>(str),std::istream_iterator<double>(),                     std::ostream_iterator<double>(std::cout,", "));std::cout<<'\n';}

      Output:

      0.11, 0.33, 0.66, 1.1,

      [edit]See also

      output iterator that writes tostd::basic_streambuf
      (class template)[edit]
      input iterator that reads fromstd::basic_istream
      (class template)[edit]
      (library fundamentals TS v2)
      an output iterator that writes successive elements into an output stream, separating adjacent elements with a delimiter
      (class template)
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/iterator/ostream_iterator&oldid=165560"

      [8]ページ先頭

      ©2009-2025 Movatter.jp