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

std::inserter

STD::插入器

Defined in header
template< class Container > std::insert_iterator<Container> inserter( Container& c, typename Container::iterator i

inserter是构造std::insert_iterator用于集装箱c及其迭代器i从参数类型推导出的类型。

参数

c-container that supports a insert operation
i-iterator in c indicating the insertion position

返回值

std::insert_iterator可用于将元素插入容器中。c在以下位置i...

可能的实施

模板<class容器>std::INSERT[医]迭代器<Container>插入器%28容器&c,类型名称容器::iterator I%29{返回std::INSERT[医]迭代器<Container>%28c,i%29;}

*。

二次

#include <algorithm> #include <iostream> #include <iterator> #include <vector> #include <set> int main() { std::multiset<int> s {1, 2, 3}; // std::inserter is commonly used with sets std::fill_n(std::inserter(s, s.end()), 5, 2 for (int n : s) std::cout << n << ' '; std::cout << '\n'; std::vector<int> d {100, 200, 300}; std::vector<int> l {1, 2, 3, 4, 5}; // when inserting in a sequence container, insertion point advances // because each std::insert_iterator::operator= updates the target iterator std::copy(d.begin(), d.end(), std::inserter(l, std::next(l.begin())) for (int n : l) std::cout << n << ' '; std::cout << '\n'; }

二次

产出:

二次

1 2 2 2 2 2 2 3 1 100 200 300 2 3 4 5

二次

另见

insert_iteratoriterator adaptor for insertion into a container (class template)
back_insertercreates a std::back_insert_iterator of type inferred from the argument (function template)
front_insertercreates a std::front_insert_iterator of type inferred from the argument (function template)

© cppreference.com

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

http://en.cppreference.com/w/cpp/iterator/INSERTER