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] ...
随机推荐
- xml的生成和发送
s2014-04-07 10:01:05 之前学的是解析,现在需要生成xml, 然后利用蓝牙或者wifi发送到服务器 2014-04-07 10:36:34 采用dom4j创建xml报错 后来发现安卓 ...
- Oracle数据库常用技术
一.视图(重点) 什么是视图? ·视图由一个或多个表(或视图)中提取数据而成 ·视图是一种虚拟表 ·视图一经创建,可以当作表来使用. 使用视图的好处? · 简化复杂数据查询 · 提高运行效率 · 屏蔽 ...
- RACSingle 有效的两种方式
第一种当然是subscribeNext 另外还有一种就是作为Command的enablesingle也相当于被订阅了.
- iOS 添加导航栏两侧按钮
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"首页" style ...
- JAVA中获取当前运行的类名,方法名,行数
JAVA中获取当前运行的类名,方法名,行数 public static String getTraceInfo(){ StringBuffer sb = new StringBuffer(); Sta ...
- P2P之UDP穿透NAT的原理与实现
首先先介绍一些基本概念: NAT(Network Address Translators),网络地址转换:网络地址转换是在IP地址日益缺乏的情况下产生的,它的主要目的就是为了能够地址重用.NAT分为两 ...
- SVN官方版本下载地址
TortoiseSVN 客户端 & Language packs 语言包 : https://tortoisesvn.net/downloads.html VisualSVN 插件官方地址: ...
- java中数组的排序,直接排序,冒泡排序,插入排序
1.直接排序: public static void selectSort(int[] arr) { for (int x = 0; x < arr.length - 1; x++) { for ...
- python yield generator 详解
本文将由浅入深详细介绍yield以及generator,包括以下内容:什么generator,生成generator的方法,generator的特点,generator基础及高级应用场景,genera ...
- DEVTMPFS
devtmpfs选项保证了系统启动使用临时文件系统.不选会启动出现readonly ,无法启动