在线文档教程
C++
迭代器 | Iterator

std::insert_iterator

STD::插入[医]迭代器

Defined in header
template< class Container > class insert_iterator : public std::iterator< std::output_iterator_tag, void,void,void,void >(until C++17)
template< class Container > class insert_iterator;(since C++17)

std::insert_iteratorOutputIterator它在所提供的迭代器所指向的位置将元素插入到为其构造的容器中。容器%27sinsert()每当迭代器%28被分配给取消引用或未分配%29时,都会调用成员函数。递增std::insert_iterator是个禁止行动。

成员类型

Member typeDefinition
container_typeContainer

成员函数

(constructor)constructs a new insert_iterator (public member function)
operator=inserts an object into the associated container (public member function)
operator*no-op (public member function)
operator++operator++(int)no-op (public member function)

成员对象

Member nameDefinition
container (protected)a pointer of type Container*
iter (protected)an iterator of type Container::iterator

成员类型

Member typeDefinition
value_typevoid
difference_typevoid
pointervoid
referencevoid
iterator_categorystd::output_iterator_tag

注意:在C++17之前,这些成员类型必须通过从std::iterator<std::output_iterator_tag,void,void,void,void>...

二次

#include <vector> #include <list> #include <iostream> #include <iterator> #include <algorithm> int main() { std::vector<int> v{1,2,3,4,5}; std::list<int> l{-1,-2,-3}; std::copy(v.begin(), v.end(), // may be simplified with std::inserter std::insert_iterator<std::list<int>>(l, std::next(l.begin())) for (int n : l) std::cout << n << ' '; std::cout << '\n'; }

二次

产出:

二次

-1 1 2 3 4 5 -2 -3

二次

另见

insertercreates a std::insert_iterator of type inferred from the argument (function template)
back_insert_iteratoriterator adaptor for insertion at the end of a container (class template)
front_insert_iteratoriterator adaptor for insertion at the front of a container (class template)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/iterator/INSERT[医]迭代器