C++inserter
C++的迭代器适配器中常用的有插入迭代器(Inser Iterator)、流迭代器(Stream Iterator)和逆向迭代器(Reverse Iterator)等!
本文主要是介绍插入迭代器(Inser Iterator).下面介绍三种插入迭代器:
1.Back Inserter
原理:其内部调用push_back()
功能:在容器的尾端插入元素
限制:只有提供了push_back()成员函数的容器中,back inserter才能派上用场
适用:vector deque list
2.Front Inserter
原理:其内部调用push_front()
功能:在容器的前端插入元素
限制:只有提供了push_front()成员函数的容器中,front inserter才能派上用场
适用:deque list
3.Inserter
原理:其内部调用insert()
功能:在容器的指定位置插入元素
限制:只有提供了inset()成员函数的容器中,inserter才能派上用场. 所有STL容器都提供了inset()函数.
适用:所有STL容器
使用举例:
typedef std::list<int> IntList;
IntList intList;
IntList::const_iterator listIt;
for(int i=1;i<=9;++i)
intList.push_back(i);
std::cout<<"list data:\n";
for(listIt=intList.begin(); listIt!=intList.end(); ++listIt)
std::cout<<*listIt<<' ';
typedef std::vector<int> IntVector;
IntVector intVector;
IntVector::const_iterator vectorIt;
copy(intList.begin(),intList.end(),back_inserter(intVector)); ///back inserter
std::cout<<"\nvector data:\n";
for(vectorIt=intVector.begin(); vectorIt!=intVector.end(); ++vectorIt)
std::cout<<*vectorIt<<' ';
//vectorIt=intVector.begin();++vectorIt;
copy(intList.begin(),intList.end(),inserter(intVector,intVector.begin()+5)); //inserter
std::cout<<"\nvector data after insert:\n";
for(vectorIt=intVector.begin(); vectorIt!=intVector.end(); ++vectorIt)
std::cout<<*vectorIt<<' ';
typedef std::deque<int> IntDeque;
IntDeque intDeque;
IntDeque::const_iterator dequeIt;
//copy(intList.begin(),intList.end(),front_inserter(intDeque)); ///front inserter
copy(intList.begin(),intList.end(),inserter(intDeque,intDeque.begin()));
std::cout<<"\ndeque data:\n";
for(dequeIt=intDeque.begin(); dequeIt!=intDeque.end(); ++dequeIt)
std::cout<<*dequeIt<<' ';
typedef std::set<int> IntSet;
IntSet intSet;
IntSet::const_iterator setIt;
copy(intList.begin(),intList.end(),inserter(intSet,intSet.begin())); ///inserter
std::cout<<"\nset data:\n";
for(setIt=intSet.begin(); setIt!=intSet.end(); ++setIt)
std::cout<<*setIt<<' ';

注意:
使用inserter的时候,插入的起始位置是在指定位置的前方!
#include <vector>
#include <iostream>
#include <cstdio>
#include <functional>
#include <algorithm>
#include <iterator>
#include<list>
#include <istream>
using namespace std; int main()
{
int ia[]={,,,,,,};
vector<int>ivec(ia,ia+);
list<int>ilst; replace_copy(ivec.begin(),ivec.end(),inserter(ilst,ilst.begin()),100,0);
18
19 //replace_copy(ivec.begin(),ivec.end(),back_inserter(ilst),100,0);
20
//replace_copy(ivec.begin(),ivec.end(),)
cout<<"list:"<<endl;
for(list<int>::iterator iter=ilst.begin();iter!=ilst.end();++iter)
cout<<*iter<<" ";
return ;
}
C++inserter的更多相关文章
- ChipScope Pro Inserter - "ERROR:NgdBuild:924 - bidirect pad net '<oDRAM0_A>' is driving non-buffer primitives
解决方案: Using a IOBUF signal as a trigger for the ILA Inserter flow will cause a NGDBuild error. These ...
- STL之--插入迭代器(back_inserter,inserter,front_inserter的区别)
除了普通迭代器,C++标准模板库还定义了几种特殊的迭代器,分别是插入迭代器.流迭代器.反向迭代器和移动迭代器,定义在<iterator>头文件中,下面主要介绍三种插入迭代器(back_in ...
- 迭代器适配器(二)general inserter的实现
上节我们实现了back_inserter和front_inserter,接下来是更为普通的插入迭代器,它允许用户指定插入位置. 实现代码如下: #ifndef ITERATOR_HPP #define ...
- [Swift]LeetCode919. 完全二叉树插入器 | Complete Binary Tree Inserter
A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...
- leetcode_919. Complete Binary Tree Inserter
https://leetcode.com/problems/complete-binary-tree-inserter/ 设计一个CBTInserter,使用给定完全二叉树初始化.三个功能; CBTI ...
- [LeetCode] 919. Complete Binary Tree Inserter 完全二叉树插入器
A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...
- LeetCode 919. Complete Binary Tree Inserter
原题链接在这里:https://leetcode.com/problems/complete-binary-tree-inserter/ 题目: A complete binary tree is a ...
- 【LeetCode】919. Complete Binary Tree Inserter 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...
- [LeetCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
随机推荐
- Android 屏幕适配方案(转载)
3.百分比的引入 1.引入 其实我们的解决方案,就是在项目中针对你所需要适配的手机屏幕的分辨率各自简历一个文件夹. 如下图: 然后我们根据一个基准,为基准的意思就是: 比如480*320的分辨率为基准 ...
- Maven项目中pom文件分析
pom英文全称: project object model 1.概述 pom.xml文件描述了maven项目的基本信息,比如groupId,artifactId,version等.也可以对maven项 ...
- A股暴跌三日市值蒸发4.2万亿 股民人均浮亏超2万
A股暴跌三日市值蒸发4.2万亿 股民人均浮亏超2万 http://finance.qq.com/a/20150508/010324.htm?pgv_ref=aio2015&ptlang=205 ...
- xlrd(开excel表格)
打来表格 wb = xlrd.open_workbook('example.xlsx') 选择sheet sh=wb.sheet_by_index(序号) 表格的行数 sh.nrows 选取第m行第n ...
- Delphi 获取内存及CPU信息的函数
Uses MemoryCpuUtils;//首先引用该单元 //声明下列变量用来存储读取的数值 Var iTotalPhysics, iTotalVirtual, iTotalPageFile, iC ...
- CentOS 7 部署、连接 数据库mariadb
1.安装mariadb yum -y install mariadb* 2.开启/停止 systemctl start mariadb #启动MariaDB systemctl stop maria ...
- Coding 代码管理快速入门
当项目创建好了之后,我们该如何上传代码到 coding 上呢?Coding 网站使用“ Git 仓库”(类似 github )来管理代码.其操作原理在于:利用 git 服务,将本地的项目目录下的文件同 ...
- java UUID的创建
java UUID的创建: 参考:http://blog.csdn.net/yaerfeng/article/details/7070369 可以研究一下最后的一段代码: http://spiritf ...
- jQuery事件触发和参数传递
jQuery事件触发和参数传递: 参考:http://www.jb51.net/article/36249.htm <%@ page language="java" impo ...
- FMS配置小结
官方连接:http://help.adobe.com/en_US/flashmediaserver/configadmin/WS5b3ccc516d4fbf351e63e3d119f2925e64-8 ...