首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::inserter

Defined in header <iterator>

?

?

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;}

*。

二次

代码语言:javascript
复制
#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';
}

二次

产出:

二次

代码语言:javascript
复制
1 2 2 2 2 2 2 3 
1 100 200 300 2 3 4 5

二次

另见

insert_iterator

iterator adaptor for insertion into a container (class template)

back_inserter

creates a std::back_insert_iterator of type inferred from the argument (function template)

front_inserter

creates a std::front_insert_iterator of type inferred from the argument (function template)

代码语言:txt
复制
 ? cppreference.com

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com