转自: http://www.cplusplus.com/reference/std/iterator/back_insert_iterator/

back_insert_iterator

<iterator>
template <class Container> class back_insert_iterator;
Back insert iterator

Back insert iterators are a special output iterator class designed to allow algorithms that usually overwrite elements (such as copy) to instead insert new elements at the end of the container.

The container must have member push_back defined (such as standard containersvector,deque and list).

Using the assignment operator on the back_insert_iterator, even when dereferenced, causes the container to append a new element to it. The other typical operators of anoutput iterator are also defined forback_insert_iterator but have no effect.

It is defined with an identical operation to:

template <class Container>
class back_insert_iterator :
public iterator<output_iterator_tag,void,void,void,void>
{
protected:
Container* container;
public:
typedef Container container_type;
explicit back_insert_iterator (Container& x) : container(x) {}
back_insert_iterator<Container>& operator= (typename Container::const_reference value)
{ container->push_back(value); return *this; }
back_insert_iterator<Container>& operator* ()
{ return *this; }
back_insert_iterator<Container>& operator++ ()
{ return *this; }
back_insert_iterator<Container> operator++ (int)
{ return *this; }
};

The library provides a function, called back_inserter, that automatically generates a back_insert_iterator class from a container.

template<typename InputIterator,typename InputIterator,typename OutputIterator>OutputIterator copy
(InputIterator first,InputIterator last,OutputInterator x{
for(;first!=last;++x,++first)*x=*first;
return(x);
}

Member functions

constructor
back_insert_iterator objects are constructed from a container.
operator=
Inserts a new element in the container, initializing its value to a copy of the argument.
operator*
Does nothing. Returns a reference to the object.
operator++
Does nothing. Returns a reference to the object.

Example

// back_insert_iterator example
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main () {
vector<int> firstvector, secondvector;
for (int i=1; i<=5; i++)
{ firstvector.push_back(i); secondvector.push_back(i*10); }
back_insert_iterator< vector<int> > back_it (firstvector);
copy (secondvector.begin(),secondvector.end(),back_it);
vector<int>::iterator it;
for ( it = firstvector.begin(); it!= firstvector.end(); ++it )
cout << *it << " ";
cout << endl;
return 0;
}

Output:

1 2 3 4 5 10 20 30 40 50

back_insert_iterator相关推荐

  1. 迭代器适配器{(插入迭代器back_insert_iterator)、IO流迭代器(istream_iterator、ostream_iterator)}...

    一.迭代器适配器 反向迭代器 插入迭代器 IO流迭代器 其中反向迭代器可以参考以前的文章. 二.插入迭代器 插入迭代器实际上是一个输出迭代器(*it=; ++) back_insert_iterato ...

  2. Effective STL 50条有效使用STL的经验笔记

    Scott Meyers大师Effective三部曲:Effective C++.More Effective C++.Effective STL,这三本书出版已很多年,后来又出版了Effective ...

  3. C++/C++11中头文件iterator的使用

    <iterator>是C++标准程序库中的一个头文件,定义了C++ STL标准中的一些迭代器模板类,这些类都是以std::iterator为基类派生出来的.迭代器提供对集合(容器)元素的操 ...

  4. STL笔记(5)条款49:学习破解有关STL的编译器诊断信息

    STL笔记(5)条款49:学习破解有关STL的编译器诊断信息 条款49:学习破解有关STL的编译器诊断信息 用一个特定的大小定义一个vector是完全合法的, vector<int> v( ...

  5. 当谈论迭代器时,我谈些什么?

    作者 | 樱雨楼 编辑 | 豌豆花下猫 转载自python猫(ID:python_cat) 导语:之前说过,我对于编程语言跟其它学科的融合非常感兴趣,但我还说漏了一点,就是我对于 Python 跟其它 ...

  6. C++知识点29——使用C++标准库(迭代器适配器)

    在上一篇文章https://blog.csdn.net/Master_Cui/article/details/108512730谈到的迭代器是基本的五种类型的迭代器 但是随着C++标准库的扩展,又实现 ...

  7. ###STL学习--标准模板库

    下面进行STL的学习.希望能了解标准模板库中的常用容器,迭代器,可以自由运用STL以提高编写代码的效率.下面的内容我想以知识点为总结,不再像<Effective C++>那样以章节进行总结 ...

  8. XCTF easyCpp buu [MRCTF2020]EasyCpp

    文章目录 easyCpp transform分析 accumulate分析 代码实现 [MRCTF2020]EasyCpp main::{lambda(int &)#1} lambda(int ...

  9. 《C++标准程序库》笔记之二

    <C++标准程序库>笔记之二 本篇博客笔记顺序大体按照<C++标准程序库(第1版)>各章节顺序编排. ------------------------------------- ...

  10. stl 之 copy copy_backward

    http://blog.csdn.net/jerryjbiao/article/details/7381498 元素复制算法copy 该算法主要用于容器之间元素的拷贝,即将迭代器区间[first,la ...

最新文章

  1. JavaEE——JavaScript
  2. WPF MvvmLight简单实例(1) 页面导航
  3. byte 和16进制关系
  4. 状态很差时,就不要再继续做了。因为是徒劳,再就是过段时间或第二天再看,会豁然开朗,原来存在的疑难问题都迎刃而解。平时没事的时候要不断地给自己充电,让自己了解多方面知识。...
  5. 第三次学JAVA再学不好就吃翔(part5)--基础语法之数据类型转换
  6. 洛谷 [P1265] 公路修建
  7. java异常总结---1.java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.support.OpenSessio
  8. ssis 映射列 使用变量_SSIS中的动态列映射:SqlBulkCopy类与数据流
  9. 对PostgreSQL Nested Loop 执行计划的初步学习
  10. php和python-一个身居中国的法国程序员谈Python与PHP
  11. eclipse的全局替换快捷键
  12. 解决win 7的Aero Peek无效的方法
  13. Blender学习笔记:齿轮模型建立
  14. Windows UWF 实现系统重启还原(2021.11.02)
  15. UTAU中文版安装教程
  16. Android studio第一次作业:实现Wechat界面
  17. Jarvis OJ BASIC 公倍数
  18. 美国人初学编程代码之三
  19. cinamon桌面添加xkill快捷键
  20. Grunt搭建自动化web前端开发环境--完整流程

热门文章

  1. webstorm绝对路径引入echarts_Webstorm+Webpack+echarts构建个性化定制的数据可视化图表两个echarts详细教程(柱状图,南丁格尔图)...
  2. 微信小程序之weui的引用及使用说明
  3. matlab 中gradient()函数的用法
  4. DOSBox常用快捷键
  5. spring扫描出现Annotation-specified bean name 'userService' for bean class [com.test.service.UserService]
  6. powershell ip_如何使用PowerShell更改IP地址
  7. RStudio不在Plots中显示图片的一个原因
  8. Spring之@Required注解
  9. Windows Server 2012 之NIC组合(NIC Teaming)介绍
  10. pick_types()函数及参数 meg eeg stim eog ecg