STL: fill,fill_n,generate,generate_n
fill
Assigns the same new value to every element in a specified range.
template<class ForwardIterator, class Type>
void fill(
ForwardIterator _First,
ForwardIterator _Last,
const Type& _Val
);
fill_n
Assigns a new value to a specified number of elements in a range beginning with a particular element.
template<class OutputIterator, class Size, class Type>
void fill_n(
OutputIterator _First,
Size _Count,
const Type& _Val
);
注:_Count必须小于等于_Last-_First.
此外,对于char* 或者wchar_t*的数组,最好是使用memset或wmemset。
generate
Assigns the values generated by a function object to each element in a range.
template<class ForwardIterator, class Generator>
void generate(
ForwardIterator _First,
ForwardIterator _Last,
Generator _Gen
);
例如,用随机数值填充vector。

template<class T>
struct display
{
void operator()(const T& val){
cout<<val<<' ';
}
}; int rand100(){
return rand()%100;
} int main()
{
vector<int> vec(5);
srand(time(0));
generate(vec.begin(),vec.end(),rand100);
cout<<"vector: ";
for_each(vec.begin(),vec.end(),display<int>());
cout<<endl; getchar();
}

generate_n
Assigns the values generated by a function object to a specified number of elements in a range and returns to the position one past the last assigned value.
template<class OutputIterator, class Diff, class Generator>
void generate_n(
OutputIterator _First,
Diff _Count,
Generator _Gen
);
此外,<numeric>中的iota可以使用递增序列填充一个指定的区域。这个函数好像是新增的,不晓得是不是标准STL中的成员。
iota
Stores a starting value, beginning with the first element and filling with successive increments of that value (_Value++) in each of the elements in the interval [_First, _Last).
template<class ForwardIterator, class Type>
void iota(
ForwardIterator _First,
ForwardIterator _Last,
Type _Value
);
参考:http://www.cnblogs.com/freewater/archive/2013/03/07/2947614.html
http://blog.csdn.net/mashen1989/article/details/7693348
STL: fill,fill_n,generate,generate_n的更多相关文章
- C++ fill fill_n generate generate_n
#include <iostream>#include <algorithm>#include <vector>#include <list>#incl ...
- 填充整个区间(fill,fill_n,generate和generate_n)
fill 将value值填充整个区间,不能为OutputIterator,因为fill会用到first和last,outputIterator无法做相等的测试 template <class F ...
- STL六大组件之——算法小小小小的解析
参考自侯捷的<stl源码剖析> stl算法主要分为非可变序列算法(指不直接修改其所操作的容器内容的算法),可变序列算法(指可以修改它们所操作的容器内容的算法),排序算法(包括对序列进行排序 ...
- 变易算法 - STL算法
欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/mutating-algorithms.h ...
- STL常用遍历算法for_each和transform的比较
for_each()和transform()算法比较 1)STL 算法 – 修改性算法 for_each() copy() copy_backward() transform() merge ...
- C++ STL 常用遍历算法
C++ STL 常用遍历算法 STL的容器算法迭代器的设计理念 1) STL的容器通过类模板技术,实现数据类型和容器模型的分离 2) STL的迭代器技术实现了遍历容器的统一方法:也为STL的算法提供了 ...
- STL经常使用遍历算法for_each和transform的比較
for_each()和transform()算法比較 1)STL 算法 – 改动性算法 for_each() copy() copy_backward() transform() merge ...
- STL--STL和她的小伙伴们:
STL--概述: 标准模板库(StandardTemplateLibrary,STL),是C++程序设计语言标准模板库.STL是由Alexander Stepanov.Meng Lee和David R ...
- cb41a_c++_STL_算法_填充新值fill_generate
cb41a_c++_STL_算法_填充新值fill_generatefill(b,e,v)fill_n(b,n,v),填充n个vgenerate(b,e,p)generate_n(b,n,p) gen ...
随机推荐
- ubuntu 按键替换 Control_R to Left
ubuntu 按键替换 Control_R to Left 1 查看当前键盘布局 $xmodmap -pke keycode 105 = Control_R NoSymbol Control_Rkey ...
- 002Maven_第一个Maven演示
第一步. 首先建立Hello项目同时建立Maven约定的目录结构 Hello --src -----main ----------java ----------reso ...
- php -- 设计模式 之 单例模式
实现单例的条件:三私一公 三私:私有化构造方法:不让外部创建对象 私有化克隆方法:不让外部克隆对象 私有静态属性:保存已经产生的对象 一公:公共静态方法:在类内部创建对象 实例: <?php / ...
- 小结:A* & IDA* & 迭代深搜
概要: 在dfs中,如果答案的深度很小但是却很宽,而且bfs还不一定好做的情况下,我们就综合bfs的优点,结合dfs的思想,进行有限制的dfs.在这里A*.IDA*和迭代深搜都是对dfs的优化,因此放 ...
- python3.4 百度API接口
# -*- coding: utf-8 -*- import urllib.request, json url = 'http://apis.baidu.com/netpopo/illegaladdr ...
- Hive将txt、csv等文本文件导入hive表
1.将txt文本文件放置hdfs目录下 2.登录hive并进入到指定数据库 3.创建表 create external table if not exists fun_user_external ( ...
- leetcode -- Best Time to Buy and Sell Stock III TODO
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Git 安装与使用
http://blog.csdn.net/lishuo_os_ds/article/details/8078475#sec-1.8.2 http://blog.csdn.net/showhilllee ...
- tomcat中文配置
tomcat传递中文乱码,修改server.xml文件 <Connector port=" protocol="HTTP/1.1" connectionTimeou ...
- js如何遍历并取出对象的属性名?
js如何遍历并取出对象的属性名? dataObj = {name : su,age : 26,height : 18cm }; for(var st in dataObj) {console.dir( ...