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. 查看Linux连接数

    netstat  -ant | grep ESTABLISHED  | wc -l

  2. leetcode--010 Linked List Cycle II

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAApAAAACICAIAAADfzUzYAAANeklEQVR4nO3dQa7bthbG8W4mK/A+so

  3. 试水mongodb er

    1)data ready var a = {"name":"zhekou","CharDate":"2015-12-01" ...

  4. IOS即时通讯XMPP搭建openfire服务器 分类: ios技术 2015-03-07 11:30 53人阅读 评论(0) 收藏

    一.下载并安装openfire 1.到http://www.igniterealtime.org/downloads/index.jsp下载最新openfire for mac版 比如:Openfir ...

  5. 编译Uboot时提示error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory

    在Ubuntu14.04 64位系统中已经安装了libc6:i386的库,编译Uboot时提示error while loading shared libraries: libz.so.1: cann ...

  6. 再次写了第一个servlet

    费时2小时,熟悉tomcat和编写了第一个servlet

  7. ASP.NET常用的控件怎么添加

    http://jingyan.baidu.com/article/d8072ac4224747ec95cefda2.html

  8. Motion-Based Multiple Object Tracking

    kalman filter tracking... %% Motion-Based Multiple Object Tracking % This example shows how to perfo ...

  9. 2.13.2. 对结果集进行筛选(Core Data 应用程序实践指南)

    Core Data通过谓词(NSPredicate)来筛选,比如限定获取的数量等.谓词基本对存储区不敏感,但也有例外,比如:matches可用在 in-memory存储区,但是不能用在SQLite存储 ...

  10. swift 中使用OC第三方库(以AFNetworking为例)

    首先呢  把你需要的第三方库导入到你的项目中来  具体怎么导入 这不是这篇的重点  看上一篇 废话不多  直接上 (1)在项目中直接建一个 oc 的控制器  然后xcode会提醒你  要不要建造桥接文 ...