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的更多相关文章

  1. 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 ...

  2. STL之--插入迭代器(back_inserter,inserter,front_inserter的区别)

    除了普通迭代器,C++标准模板库还定义了几种特殊的迭代器,分别是插入迭代器.流迭代器.反向迭代器和移动迭代器,定义在<iterator>头文件中,下面主要介绍三种插入迭代器(back_in ...

  3. 迭代器适配器(二)general inserter的实现

    上节我们实现了back_inserter和front_inserter,接下来是更为普通的插入迭代器,它允许用户指定插入位置. 实现代码如下: #ifndef ITERATOR_HPP #define ...

  4. [Swift]LeetCode919. 完全二叉树插入器 | Complete Binary Tree Inserter

    A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...

  5. leetcode_919. Complete Binary Tree Inserter

    https://leetcode.com/problems/complete-binary-tree-inserter/ 设计一个CBTInserter,使用给定完全二叉树初始化.三个功能; CBTI ...

  6. [LeetCode] 919. Complete Binary Tree Inserter 完全二叉树插入器

    A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...

  7. LeetCode 919. Complete Binary Tree Inserter

    原题链接在这里:https://leetcode.com/problems/complete-binary-tree-inserter/ 题目: A complete binary tree is a ...

  8. 【LeetCode】919. Complete Binary Tree Inserter 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...

  9. [LeetCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

随机推荐

  1. make执行过程

    转载自 陈皓<跟我一起写 Makefile> 一般来说,最简单的就是直接在命令行下输入make命令,make命令会找当前目录的makefile来执行,一切都是自动的.但也有时你也许只想让m ...

  2. UVa 11059 最大乘积

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. UVa 412 - Pi

    题目大意:给定一种估算Pi的方法:给出一系列随机数,从中任选两个数,这两个数的最大公约数不大于1(互质)的概率为6/(Pi*Pi),然后给出一系列数,据此估算Pi的值.直接模拟就好了. #includ ...

  4. 实时 Django 终于来了 —— Django Channels 入门指南

    Reference: http://www.oschina.net/translate/in_deep_with_django_channels_the_future_of_real_time_app ...

  5. ASCII 码对应表

    Macron symbol ASCII CODE 238 : HTML entity : [ Home ][ español ] What is my IP address ? your public ...

  6. Latex 的Table使用multirow and multicolumn

    Here are some tips: Use a \multicolumn{1}{|l}{} & \multicolumn{2}{l|}{...} construct; Add anothe ...

  7. SpringMVC 前端获得定义JSON对象的方法

    SpringMVC 前端获得定义JSON对象的方法: 可以使用map进行对象的创建,这样就会解析成键值对,不需要为前端专门定义对象.

  8. 负载均衡软件LVS分析三(配置)

    LVS集群有DR.TUN.NAT三种配置模式,可以对www服务.FTP服务.MAIL服务等做负载均衡,下面通过搭建www服务的负载均衡实例,讲述基于DR模式的LVS集群配置. 一. Director ...

  9. Java中的封装、继承、多态

    封装 在如何理解面向对象这篇文章中,提到所谓的封装就是"功能都给你做好了,你不必去理解它是怎么写出来的,直接使用即可.".但你得清楚一点,那就是这句话是相对于使用者来说的,而作为开 ...

  10. JavaScript中DOM的层次节点(一)

    DOM是针对HTML和XML文档的一个API,描绘了一个层次化的节点树,允许开发人员添加.修改.删除节点的一部分. DOM将HTML和XML文档描绘成一个有多个节点构成的结构,节点分为12种不同的节点 ...