C++11标准模板(STL)- 算法(std::iota)
定义于头文件 <algorithm>
算法库提供大量用途的函数(例如查找、排序、计数、操作),它们在元素范围上操作。注意范围定义为 [first, last)
,其中 last
指代要查询或修改的最后元素的后一个元素。
用从起始值开始连续递增的值填充一个范围
std::iota
template< class ForwardIterator, class T > |
(C++11 起) |
以始于 value
并重复地求值 ++value 的顺序递增值填充范围 [first, last)
。
等价操作:
*(d_first) = value;
*(d_first+1) = ++value;
*(d_first+2) = ++value;
*(d_first+3) = ++value;
...
参数
first, last | - | 以 value 开始,按顺序递增填充的值的范围 |
value | - | 要存储的初始值,表达式 ++value 必须为良式 |
返回值
(无)
复杂度
正好 last - first
次自增与赋值。
可能的实现
template<class ForwardIterator, class T>
void iota(ForwardIterator first, ForwardIterator last, T value)
{while(first != last) {*first++ = value;++value;}
}
调用示例
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <list>
#include <iterator>
#include <time.h>using namespace std;struct Cell
{int x;int y;Cell &operator +=(const Cell &cell){x += cell.x;y += cell.y;return *this;}Cell &operator ++(){x += 1;y += 1;return *this;}bool operator <(const Cell &cell) const{if (x == cell.x){return y < cell.y;}else{return x < cell.x;}}bool operator ==(const Cell &cell) const{return x == cell.x && y == cell.y;}
};std::ostream &operator<<(std::ostream &os, const Cell &cell)
{os << "{" << cell.x << "," << cell.y << "}";return os;
}int main()
{std::mt19937 g{std::random_device{}()};srand((unsigned)time(NULL));;std::cout << std::boolalpha;std::function<Cell()> generate = [](){int n = std::rand() % 10 + 100;Cell cell{n, n};return cell;};// 初始化cells1std::list<vector<Cell>> lCells(6, vector<Cell>(6));//用从起始值开始连续递增的值填充一个范围for (vector<Cell> &vCells : lCells){std::iota(vCells.begin(), vCells.end(), generate());}size_t index = 0;for (vector<Cell> &vCells : lCells){std::cout << "lCells " << index << " ";std::copy(vCells.begin(), vCells.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;index++;}return 0;
}
输出
C++11标准模板(STL)- 算法(std::iota)相关推荐
- 【跟学C++】C++STL标准模板库——算法详细整理(下)(Study18)
文章目录 1.简介 2.STL算法分类及常用函数 2.2.变序算法(二) 2.2.1 替换算法(2个) 2.2.2 排序算法(6个) 2.2.3 分区算法(4个) 2.2.4 可用于排序容器的算法(3 ...
- 【跟学C++】C++STL标准模板库——算法详细整理(中)(Study18)
文章目录 1.简介 2.STL算法分类及常用函数 2.1.变序算法(一) 2.2.1 初始化算法(2个) 2.2.2 修改算法(2个) 2.2.3 复制算法(6个) 2.2.4 删除算法(6个) 3. ...
- c++ 11标准模板(STL) std::vector (十一)
定义于头文件 <vector> template< class T, class Allocator = std::allocator<T> > class ...
- 【跟学C++】C++STL标准模板库——算法详细整理(上)(Study18)
文章目录 1.STL简介 2.STL算法分类及常用函数 2.1.非变序算法 2.1.1 计数算法(2个) 2.1.2 搜索算法(7个) 2.1.3 比较算法(2个) 3.总结 =========== ...
- C++11后的STL算法
文章目录 一.函数对象 二.预定义的函数对象 三.算法函数 1.自己实现foreach算法 2.自己实现的findif算法 3.自己实现bsort算法 一.函数对象 STL提供了很多处理容器的函数模板 ...
- C++11标准模板(STL)- 算法(std::set_symmetric_difference)
定义于头文件 <algorithm> 算法库提供大量用途的函数(例如查找.排序.计数.操作),它们在元素范围上操作.注意范围定义为 [first, last) ,其中 last 指代要查询 ...
- C++11标准模板(STL)- 算法(std::nth_element)
定义于头文件 <algorithm> 算法库提供大量用途的函数(例如查找.排序.计数.操作),它们在元素范围上操作.注意范围定义为 [first, last) ,其中 last 指代要查询 ...
- C++11标准模板(STL)- 算法 - 集合操作(在已排序范围上)(std::set_difference)
定义于头文件 <algorithm> 算法库提供大量用途的函数(例如查找.排序.计数.操作),它们在元素范围上操作.注意范围定义为 [first, last) ,其中 last 指代要查询 ...
- C++11标准模板(STL)- 算法(std::adjacent_difference)
定义于头文件 <algorithm> 算法库提供大量用途的函数(例如查找.排序.计数.操作),它们在元素范围上操作.注意范围定义为 [first, last) ,其中 last 指代要查询 ...
最新文章
- MyBatis批量插入几千条数据慎用foreach
- 【Android】AsyncTask异步类
- iOS安全攻防(三):使用Reveal分析他人app
- linux 分卷压缩命令,linux 分卷压缩命令
- (z) 什么是好的硬件工程师
- LG Display为防控疫情采取措施 要求去过大邱市员工两周不要来公司
- MySQL distinct多个字段
- 20、Tabs底部导航栏
- CodeForces 2B The least round way
- swift下FMDB的使用
- oracle trap,配置SNMP trap
- NTP服务器的相关配置
- matlab怎么取差分,差分进化算法原理与matlab实现
- 服务器虚拟化的几种方式,服务器虚拟化究竟应当选择哪种方式?
- NCURSES程序设计之皇后问题
- 使用代理ip会导致网络卡顿吗?
- 我的世界空岛生存服务器制作,我的世界Wishing服务器-RPG丨空岛丨生存丨[1.12.2-1.16.1]...
- Java网络 1.3 开发工具介绍
- RSA加密算法学习过程
- 【Python】《Python语言程序设计》(嵩天 、黄天羽 、礼欣)测验单项选择题答案与解析合辑