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] ...
随机推荐
- HDU 3501 Calculation 2 ——Dirichlet积
[题目分析] 卷积太有趣了. 最终得出结论,互质数和为n*phi(n)/2即可. 计算(n*(n+1)/2-n-n*phi(n)/2)%md,用反正法即可证明. [代码] #include <c ...
- Mysql mysql lost connection to server during query 问题解决方法
在linux上新安装的mysql数据库远程连接速度很慢,用Navicate可以正常连接但是时间很长:使用toad连接提示Mysql mysql lost connection to server du ...
- LIQN join on 后跟多个条件
sql 版:SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredD ...
- IOS开发中深拷贝与浅拷贝
简而言之: 1.对不可变的非集合对象,copy是指针拷贝,mutablecopy是内容拷贝 2.对于可变的非集合对象,copy,mutablecopy都是内容拷贝 3.对不可变的数组.字典.集合等集合 ...
- 通过div模拟table
参考: https://css-tricks.com/complete-guide-table-element/ 不要使用内联样式,但只是为了了解这里是如何去: <section style=& ...
- shell相关命令
1.shell是什么? 从用户角度:shell是用户与Linux沟通的桥梁 从程序员角度:shell本身是一种用C语言编写的程序 shell担任了翻译的角色,将用户输入的命令翻译成Linux能够识别的 ...
- 安卓版php服务器的mysql数据库增删改查简单案例
界面: index.php文件: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...
- Spring的IOC原理[通俗解释一下]
Spring的IOC原理[通俗解释一下] 1. IoC理论的背景我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑. 图 ...
- Failed to register Grid Infrastructure type ora.mdns.type
安装11g的集群软件的时候,在最后运行root.sh脚本时候,没有执行成功,最后提示如下错误: [root@r2 ~]# /u01/app/11.2.0/grid_1/root.sh Performi ...
- 给Pomelo的聊天室添加time的RPC调用
为了练手,给聊天应用增加一个rpc调用和一个time类型的服务器,在servers/time/remote/timeRemote.js中,添加如下代码: module.exports.getCurre ...