1,代码如下:

#include<iostream>
#include<list>
#include<algorithm>
#include<iterator>
#include<vector>
using namespace std;
class IntSequence
{
private:
    int value;
public:
    //constructor
    IntSequence(int initialValue):value(initialValue){}
    //function call
    int operator()() //注意2个括号,第一个括号表示重载
    {
        return value++;
    }
};
int main()
{
    vector<);
    //vector<int> vecList;
    generate_n(vecList.begin(),,IntSequence());//没有使用back_inserter
    copy(vecList.begin(),vecList.end(),ostream_iterator<int>(cout," "));
    cout<<endl;

    list<int> coll;
    //insert value from 1 to 9;
    generate_n(back_inserter(coll),,IntSequence());//必须使用back_inserter,否则编译通过,运行报错。
    //generate_n(coll.begin(),9,IntSequence(1));//编译通过,运行报错
    copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "));
    cout<<endl;
    //replace second to last element but one with values
    //starting at 42
    generate(++coll.begin(),--coll.end(),IntSequence());
    copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "));
    cout<<endl;
    ;
}

因为list定义的时候没有大小,即没有分配空间,插入失败报错。vector定义了大小为12,所以可以直接插入,不用通过back_inserter之类的迭代适配器。

back_inserter的用法的更多相关文章

  1. std::back_inserter函数用法

    back_inserter函数:配合copy函数,把[a, b)区间的数据插入到string对象的末尾,如果容量不够,动态扩容. 使用案例: 1.客户端与服务器通信场景:服务器向客户端发送数据,客户端 ...

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

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

  3. STL源码剖析(适配器)

    STL中由三类适配器,它们分别是: 1.容器适配器(stack.queue) 2.迭代器适配器(insert_iterator.reverse_iterator.iostream_iterator) ...

  4. c++ bind1st 和 bind2nd的用法

    std::bind1st 和 std::bind2nd将二元函数转换为一元函数,具体用法参加下面的代码. 代码介绍了两种使用方式,第一种是使用std::less和std::greater,第二种是使用 ...

  5. C++标准 bind函数用法与C#简单实现

    在看C++标准程序库书中,看到bind1st,bind2nd及bind的用法,当时就有一种熟悉感,仔细想了下,是F#里提到的柯里化.下面是维基百科的解释:在计算机科学中,柯里化(英语:Currying ...

  6. STL中mem_fun, mem_fun_ref用法

    1.引言 先看一个STL中for_each的用法: #include <iostream> #include <vector> #include <algorithm&g ...

  7. c++11之copy 和 copy_if 的用法

    0.时刻提醒自己 Note: vector的释放 1.功能 复制 [first, last) 所定义的范围中的元素到始于 d_first 的另一范围. 区别: copy_if 带条件拷贝,而非全拷贝 ...

  8. EditText 基本用法

    title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...

  9. jquery插件的用法之cookie 插件

    一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...

随机推荐

  1. CDH-5.4.3离线安装

    使用CM离线安装CDH-5.4.3,如下: cdh5.4.3安装 配置/etc/hosts vim /etc/hosts 192.168.10.1 s1 192.168.10.2 s2 192.168 ...

  2. 我所理解的设计模式(C++实现)——策略模式(Strategy Pattern)

    概述: 每个人都要“交个人所得税”,但是“在美国交个人所得税”和“在中国交个人所得税”就有不同的算税方法. 而策略模式就是对算法进行包装,是把使用算法的责任和算法本身分割开来,委派给不同的对象管理.策 ...

  3. [原创作品] web项目构建(一)

    今天开始,将推出web项目构建教程,与<javascript精髓整理篇>一并更新.敬请关注. 这篇作为这一系列开头,主要讲述web项目的构建技术大全.在众多人看来,web前端开发无非就是写 ...

  4. openwrt time sycronize

    三行命令搞定这个. opkg update opkg install ntpclient ntpclient -s -c 0 -h ntp.sjtu.edu.cn 最后把这个 放到 rc.local ...

  5. openwrt sdk compile

    recently ,bought a router : tl-wr741n-v5 hd my aim : let the router dail in neetkeeper environment : ...

  6. 用MVC4练习,后台用aspx,数据库DemoDb《MvcUserDemo》

    将ado.net的cs文件SqlHelper.cs放入解决方案 using System; using System.Collections.Generic; using System.Linq; u ...

  7. sqlserver获取当前id的前一条数据和后一条数据

    一.条件字段为数值的情况   select * from tb where id=@id; --当前记录   select top 1 * from tb where id>@id order  ...

  8. C#显示声名接口就是为了解决方法重名的问题

    class class1 { public static void Main(string[] args) { Person ps = new Person(); ps.KouLan(); IFlya ...

  9. uva 11038 - How Many O's?

    想法: 將問題簡化為求1~m 0的總數,以及1~n 0的總數,然後最後再相減. 求1~n 0的總數,要將n分別算每個位數0的個數,舉例如30324: 先從右邊第一位'4'開始,其左邊為3032,表示1 ...

  10. angular json转义html

    第一步json输出的内容反编码 function htmldecode(s){ var div = document.createElement('div'); div.innerHTML = s; ...