cpp/iterator/back insert iterator: Difference between revisions
From cppreference.com
m link concept, shorten a line |
m fmt. |
||
| (19 intermediate revisions by 14 users not shown) | |||
| Line 1: | Line 1: | ||
{{cpp/title|back_insert_iterator}} | {{cpp/title|back_insert_iterator}} | ||
{{cpp/iterator/back_insert_iterator/navbar}} | {{cpp/iterator/back_insert_iterator/navbar}} | ||
{{ | {{begin}} | ||
{{ | {{header|iterator}} | ||
{{ | {{ | ||
|1= | |||
template< class Container > | template< class Container > | ||
class back_insert_iterator : public std::iterator< std::output_iterator_tag, | class back_insert_iterator | ||
: public std::iterator<std::output_iterator_tag, void, void, void, void> | |||
}} | }} | ||
{{ | {{ | ||
end}} | |||
{{tt|std::back_insert_iterator}} is | {{tt|std::back_insert_iterator}} is {{|OutputIterator}} that appends to a container for which it was constructedcontainer's {{tt|push_back()}} member function whenever the iterator (whether dereferenced or not) is assigned to. Incrementing the {{tt|std::back_insert_iterator}} is a no-op. | ||
{{|| | |||
{{ | {{|{{tt|container_type}}|{{tt|Container}}}} | ||
}} | |||
{{ | |||
===Member functions=== | ===Member functions=== | ||
{{ | {{begin}} | ||
{{ | {{|cpp/iterator/inserter/constructor|back_insert_iterator}} | ||
{{ | {{|cpp/iterator/inserter/operator{{=}}|back_insert_iterator}} | ||
{{ | {{|cpp/iterator/inserter/operator*|back_insert_iterator}} | ||
{{ | {{|cpp/iterator/inserter/|back_insert_iterator}} | ||
{{ | {{end}} | ||
===Member objects=== | ===Member objects=== | ||
{{ | {{begin}} | ||
{{ | {{hitem|Member name|Definition}} | ||
{{ | {{|{{tt|container}} {{mark|protected}}|a pointer of type {{tt|Container*}}}} | ||
{{ | {{end}} | ||
===Example=== | ===Example=== | ||
{{example | {{example | ||
|code= | |||
#include <iostream> | #include <iostream> | ||
#include <iterator> | #include <iterator> | ||
#include < | #include <> | ||
int main() | int main() | ||
{ | { | ||
std::vector<int> v; | std::vector<int> v; | ||
std::generate_n(std::back_insert_iterator<std::vector<int>>(v), // | |||
std::generate_n( | |||
for(int n : v) | std::back_insert_iterator<std::vector<int>>(v), | ||
// | |||
10, | |||
[]() { return ; } | |||
); | |||
for (int n : v) | |||
std::cout << n << ' '; | std::cout << n << ' '; | ||
std::cout << '\n'; | std::cout << '\n'; | ||
} | } | ||
|output= | |||
3 6 7 | 3 6 7 9 | ||
}} | }} | ||
===See also=== | ===See also=== | ||
{{ | {{begin}} | ||
{{ | {{|cpp/iterator/back_inserter}} | ||
{{ | {{|cpp/iterator/front_insert_iterator}} | ||
{{ | {{|cpp/iterator/insert_iterator}} | ||
{{ | {{end | ||
}} | |||
Latest revision as of 16:26, 18 September 2023
| Defined in header <iterator>
|
||
template< class Container >
class back_insert_iterator
: public std::iterator<std::output_iterator_tag, void, void, void, void>
|
(until C++17) | |
template< class Container >
class back_insert_iterator;
|
(since C++17) | |
std::back_insert_iterator is a LegacyOutputIterator that appends elements to a container for which it was constructed. The container's push_back() member function is called whenever the iterator (whether dereferenced or not) is assigned to. Incrementing the std::back_insert_iterator is a no-op.
Member types
| Member type | Definition | ||||
iterator_category
|
std::output_iterator_tag
| ||||
value_type
|
void
| ||||
difference_type
|
| ||||
pointer
|
void
| ||||
reference
|
void
| ||||
container_type
|
Container
|
|
Member types |
(until C++17) |
Member functions
constructs a new back_insert_iterator (public member function) | |
| inserts an object into the associated container (public member function) | |