Difference between revisions of "cpp/iterator/ostream iterator"
m (Text replace - "{{concept" to "{{named req") |
(combine member types) |
||
Line 25: | Line 25: | ||
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. | 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. | ||
− | + | {{cpp/iterator/iterator/inherit|std::output_iterator_tag|void|void|void|void|ext= | |
− | {{ | + | |
− | + | ||
{{dsc | {{tt|char_type}} | {{tt|CharT}}}} | {{dsc | {{tt|char_type}} | {{tt|CharT}}}} | ||
{{dsc | {{tt|traits_type}} | {{tt|Traits}}}} | {{dsc | {{tt|traits_type}} | {{tt|Traits}}}} | ||
{{dsc | {{tt|ostream_type}} | {{c|std::basic_ostream<CharT, Traits>}}}} | {{dsc | {{tt|ostream_type}} | {{c|std::basic_ostream<CharT, Traits>}}}} | ||
− | + | }} | |
===Member functions=== | ===Member functions=== | ||
Line 41: | Line 39: | ||
{{dsc inc | cpp/iterator/ostream_iterator/dsc operator_arith}} | {{dsc inc | cpp/iterator/ostream_iterator/dsc operator_arith}} | ||
{{dsc end}} | {{dsc end}} | ||
− | |||
− | |||
===Example=== | ===Example=== | ||
Line 71: | Line 67: | ||
{{dsc end}} | {{dsc end}} | ||
− | + | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
Revision as of 00:06, 15 June 2019
Defined in header <iterator>
|
||
template< class T, class CharT = char, |
(until C++17) | |
template< class T, class CharT = char, |
(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 |
Member types
Member type | Definition | ||||
iterator_category
|
std::output_iterator_tag | ||||
value_type
|
void | ||||
difference_type
|
| ||||
pointer
|
void | ||||
reference
|
void | ||||
char_type
|
CharT
| ||||
traits_type
|
Traits
| ||||
ostream_type
|
std::basic_ostream<CharT, Traits> |
Member types |
(until C++17) |
Member functions
constructs a new ostream_iterator (public member function) | |
destructs an ostream_iterator (public member function) | |
writes an object to the associated output sequence (public member function) | |
no-op (public member function) | |
no-op (public member function) |
Example
#include <iostream> #include <sstream> #include <iterator> #include <numeric> 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, " ")); }
Output:
0.1 0.3 0.6 1
See also
output iterator that writes to std::basic_streambuf (class template) | |
input iterator that reads from std::basic_istream (class template) | |
(library fundamentals TS v2) |
An output iterator that writes successive elements into an output stream, separating adjacent elements with a delimiter (class template) |