Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/iterator/ostream iterator"

From cppreference.com
< cpp‎ | iterator
m
m (fmt)
 
(19 intermediate revisions by 12 users not shown)
Line 1: Line 1:
 
{{cpp/title|ostream_iterator}}
 
{{cpp/title|ostream_iterator}}
 
{{cpp/iterator/ostream_iterator/navbar}}
 
{{cpp/iterator/ostream_iterator/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list header | iterator}}
+
{{dcl header|iterator}}
{{ddcl list item | 1=
+
{{dcl rev begin}}
 +
{{dcl|until=c++17|1=
 
template< class T,
 
template< class T,
 
           class CharT = char,
 
           class CharT = char,
           class Traits = std::char_traits<charT>>
+
           class Traits = std::char_traits<CharT> >
class ostream_iterator : public std::iterator<std::output_iterator_tag,
+
class ostream_iterator
                                              void, void, void, void>
+
    : public std::iterator<std::output_iterator_tag, void, void, void, void>
 
}}
 
}}
{{ddcl list end}}
+
{{dcl|since=c++17|1=
 +
template< class T,
 +
          class CharT = char,
 +
          class Traits = std::char_traits<CharT> >
 +
class ostream_iterator;
 +
}}
 +
{{dcl end}}
  
{{tt|std::ostream_iterator}} is a single-pass output iterator that writes successive objects of type {{tt|T}} into the {{c|std::basic_ostream}} object for which it was constructed, using {{tt|operator<<}}. 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 the {{tt|std::ostream_iterator}} is a no-op.
+
{{tt|std::ostream_iterator}} is a single-pass {{named req|OutputIterator}} that writes successive objects of type {{tt|T}} into the {{lc|std::basic_ostream}} object for which it was constructed, using {{tt|operator<<}}. 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 the {{tt|std::ostream_iterator}} is a no-op.
  
 
In a typical implementation, the only data members of {{tt|std::ostream_iterator}} are a pointer to the associated {{tt|std::basic_ostream}} and a pointer to the first character in the delimiter string.
 
In a typical implementation, the only data members of {{tt|std::ostream_iterator}} are a pointer to the associated {{tt|std::basic_ostream}} and a pointer to the first character in the delimiter string.
  
When writing characters, {{c|std::ostreambuf_iterator}} is more efficient, since it avoids the overhead of constructing and destructing the sentry object once per character.
+
When writing characters, {{lc|std::ostreambuf_iterator}} is more efficient, since it avoids the overhead of constructing and destructing the sentry object once per character.
  
===Member types===
+
{{cpp/iterator/iterator/inherit|std::output_iterator_tag|void|void|void|void|ext=
{{dcl list begin}}
+
{{dsc|{{tt|char_type}}|{{tt|CharT}}}}
{{dcl list hitem | Member type | Definition}}
+
{{dsc|{{tt|traits_type}}|{{tt|Traits}}}}
{{dcl list item | {{tt|char_type}} | {{tt|CharT}}}}
+
{{dsc|{{tt|ostream_type}}|{{c/core|std::basic_ostream<CharT, Traits>}}}}
{{dcl list item | {{tt|traits_type}} | {{tt|Traits}}}}
+
}}
{{dcl list item | {{tt|ostream_type}} | {{c|std::basic_ostream<CharT, Traits>}}}}
+
{{dcl list end}}
+
  
 
===Member functions===
 
===Member functions===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list mem ctor | cpp/iterator/ostream_iterator/ostream_iterator | constructs a new ostream_iterator }}
+
{{dsc inc|cpp/iterator/ostream_iterator/dsc constructor}}
{{dcl list mem dtor | cpp/iterator/ostream_iterator/~ostream_iterator | destructs an ostream_iterator}}
+
{{dsc inc|cpp/iterator/ostream_iterator/dsc destructor}}
{{dcl list mem fun | cpp/iterator/ostream_iterator/operator{{=}} | writes a object to the associated output sequence }}
+
{{dsc inc|cpp/iterator/ostream_iterator/dsc operator{{=}}}}
{{dcl list mem fun | cpp/iterator/ostream_iterator/operator* | no-op }}
+
{{dsc inc|cpp/iterator/ostream_iterator/dsc operator*}}
{{dcl list mem fun | cpp/iterator/ostream_iterator/operator++ | title=operator++<br>operator++{{dcl small|(int)}} | no-op }}
+
{{dsc inc|cpp/iterator/ostream_iterator/dsc operator_arith}}
{{dcl list end}}
+
{{dsc end}}
 
+
{{cpp/iterator/iterator/inherit|std::output_iterator_tag|void|void|void|void}}
+
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
 
#include <iostream>
 
#include <iostream>
#include <sstream>
 
 
#include <iterator>
 
#include <iterator>
#include <algorithm>
+
#include <numeric>
 +
#include <sstream>
  
 
int main()
 
int main()
 
{
 
{
     std::istringstream str("0.1 0.2 0.3 0.4");
+
     std::istringstream str("0.11 0.22 0.33 0.44");
 +
 
 
     std::partial_sum(std::istream_iterator<double>(str),
 
     std::partial_sum(std::istream_iterator<double>(str),
                      std::istream_iterator<double>(),
+
                    std::istream_iterator<double>(),
                      std::ostream_iterator<double>(std::cout, " "));
+
                    std::ostream_iterator<double>(std::cout, ", "));
 +
    std::cout << '\n';
 
}
 
}
| output=
+
|output=
0.1 0.3 0.6 1  
+
0.11, 0.33, 0.66, 1.1,
 
}}
 
}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/iterator/dcl list ostreambuf_iterator}}
+
{{dsc inc|cpp/iterator/dsc ostreambuf_iterator}}
{{dcl list template | cpp/iterator/dcl list istream_iterator}}
+
{{dsc inc|cpp/iterator/dsc istream_iterator}}
{{dcl list end}}
+
{{dsc tclass|cpp/experimental/ostream_joiner|title=std::experimental::ostream_joiner|an output iterator that writes successive elements into an output stream, separating adjacent elements with a delimiter|notes={{mark since libfund_ts_2}}}}
 +
{{dsc end}}
  
[[de:cpp/iterator/ostream iterator]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/iterator/ostream iterator]]
+
[[fr:cpp/iterator/ostream iterator]]
+
[[it:cpp/iterator/ostream iterator]]
+
[[ja:cpp/iterator/ostream iterator]]
+
[[pt:cpp/iterator/ostream iterator]]
+
[[ru:cpp/iterator/ostream iterator]]
+
[[zh:cpp/iterator/ostream iterator]]
+

Latest revision as of 00:16, 12 December 2023

 
 
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

    : public std::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-pass LegacyOutputIterator that writes successive objects of type T into the std::basic_ostream object for which it was constructed, using operator<<. 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 the std::ostream_iterator is a no-op.

In a typical implementation, the only data members of std::ostream_iterator are a pointer to the associated std::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_category std::output_iterator_tag
value_type void
difference_type

void

(until C++20)

std::ptrdiff_t

(since C++20)
pointer void
reference void
char_type CharT
traits_type Traits
ostream_type std::basic_ostream<CharT, Traits>

Member types iterator_category, value_type, difference_type, pointer and reference are required to be obtained by inheriting from std::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 an ostream_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

#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 to std::basic_streambuf
(class template) [edit]
input iterator that reads from std::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)