back_inserter的用法
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的用法的更多相关文章
- std::back_inserter函数用法
back_inserter函数:配合copy函数,把[a, b)区间的数据插入到string对象的末尾,如果容量不够,动态扩容. 使用案例: 1.客户端与服务器通信场景:服务器向客户端发送数据,客户端 ...
- STL之--插入迭代器(back_inserter,inserter,front_inserter的区别)
除了普通迭代器,C++标准模板库还定义了几种特殊的迭代器,分别是插入迭代器.流迭代器.反向迭代器和移动迭代器,定义在<iterator>头文件中,下面主要介绍三种插入迭代器(back_in ...
- STL源码剖析(适配器)
STL中由三类适配器,它们分别是: 1.容器适配器(stack.queue) 2.迭代器适配器(insert_iterator.reverse_iterator.iostream_iterator) ...
- c++ bind1st 和 bind2nd的用法
std::bind1st 和 std::bind2nd将二元函数转换为一元函数,具体用法参加下面的代码. 代码介绍了两种使用方式,第一种是使用std::less和std::greater,第二种是使用 ...
- C++标准 bind函数用法与C#简单实现
在看C++标准程序库书中,看到bind1st,bind2nd及bind的用法,当时就有一种熟悉感,仔细想了下,是F#里提到的柯里化.下面是维基百科的解释:在计算机科学中,柯里化(英语:Currying ...
- STL中mem_fun, mem_fun_ref用法
1.引言 先看一个STL中for_each的用法: #include <iostream> #include <vector> #include <algorithm&g ...
- c++11之copy 和 copy_if 的用法
0.时刻提醒自己 Note: vector的释放 1.功能 复制 [first, last) 所定义的范围中的元素到始于 d_first 的另一范围. 区别: copy_if 带条件拷贝,而非全拷贝 ...
- EditText 基本用法
title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...
- jquery插件的用法之cookie 插件
一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...
随机推荐
- Xshell远程连接Ubuntu
在Ubuntu系统下执行命令: sudo apt-get install openssh-server 然后,在Xshell中新建会话,输入Ubuntu系统的IP,以及用户名.密码. 但是经常会出现连 ...
- socket实例2
第二个实例创建一个java工程,基于tomcat服务器,程序运行时会启动客户端,实现了一个客户端向其他的客户端发送即时信息的功能 MainWindow.java package com.jikexue ...
- 实现Android 版网页快照功能
现在一般的购物网站,在你完成交易后都会将页面拍照以免日后发生商务纠纷,而对于我们移动开发者这个传统互联网上的优秀经验也同样给了我们一些设计上的启迪,接下来我将几种实现思路写出来供大家参考. 方案一:使 ...
- Java基础知识强化44:StringBuffer类之把数组拼接成指定格式的字符串的案例
1. 先看案例代码如下: package cn.itcast_07; /* * 把数组拼接成一个字符串 */ public class StringBufferTest2 { public stati ...
- canvas-画蜗牛
<!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...
- ado.net 数据库连接的两方式种
DataAdapter: string connectionString = "data source=127.0.0.1;Database=dong;user id=sa;password ...
- C#6.0语法糖
using System; using static System.Math;//using static,仅仅引入类中的静态方法 namespace _6._0Syntax { class Prog ...
- XCode请求数据中接收类型的后台与前台处理(本机模拟)
Xcode报错问题如下: 解决办法如下: 0x1 ->请求数据时加上缺少的类型 AFHTTPSessionManager *manager = [selfAFHTTPSessionMan ...
- 12 hdfs常用文件、目录拷贝操作、删除操作
package com.da.hbase.tool.utils; import com.da.hbase.tool.common.Const; import org.apache.hadoop.con ...
- nyoj组合数
算法:深搜 描述 找出从自然数1.2.... .n(0<n<10)中任取r(0<r<=n)个数的所有组合. 输入输入n.r.输出按特定顺序输出所有组合. 特定顺序:每一个组合中 ...