std::ranges::iota, std::ranges::iota_result
受约束算法及范围上的算法 (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
受约束算法: std::ranges::copy, std::ranges::sort, ... | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
执行策略 (C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
不修改序列的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
修改序列的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Partitioning operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
划分操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
排序操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
二分搜索操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
集合操作(在已排序范围上) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
堆操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
最小/最大操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
排列 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
数值运算 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
未初始化存储上的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
C 库 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
在标头
<numeric>
定义
|
||
调用签名
|
||
template< std::input_or_output_iterator O, std::sentinel_for<O> S,
std::weakly_incrementable T > |
(1) | (C++23 起) |
template< std::weakly_incrementable T, ranges::output_range<const T&> R >
constexpr iota_result<ranges::borrowed_iterator_t<R>, T> |
(2) | (C++23 起) |
Helper types
|
||
template< class O, class T >
using iota_result = ranges::out_value_result<O, T>; |
(3) | (C++23 起) |
用依次递增的值填充范围 [
first,
last)
,起始为 value 并重复应用 ++value 的值。
等价操作:
*(first) = value; *(first + 1) = ++value; *(first + 2) = ++value; *(first + 3) = ++value; ...
参数
first, last | - | 从 value 开始依次递增的数值填充的元素范围 |
value | - | 起始值;表达式 ++value 必须良构 |
返回值
{last, value + ranges::distance(first, last)}
复杂度
严格 last - first 次递增和赋值。
可能的实现
struct iota_fn { template<std::input_or_output_iterator O, std::sentinel_for<O> S, std::weakly_incrementable T> requires std::indirectly_writable<O, const T&> constexpr iota_result<O, T> operator()(O first, S last, T value) const { while (first != last) { *first = as_const(value); ++first; ++value; } return {std::move(first), std::move(value)}; } template<std::weakly_incrementable T, std::ranges::output_range<const T&> R> constexpr iota_result<std::ranges::borrowed_iterator_t<R>, T> operator()(R&& r, T value) const { return (*this)(std::ranges::begin(r), std::ranges::end(r), std::move(value)); } }; inline constexpr iota_fn iota; |
注解
该函数以 APL 编程语言中的整数函数 ⍳ 命名。
功能特性测试宏 | 值 | 标准 | 备注 |
---|---|---|---|
__cpp_lib_ranges_iota |
202202L | (C++23) | std::ranges::iota
|
示例
使用 vector 的迭代器(std::vector<std::list<T>::iterator)作为代理,对 std::list 中的元素进行洗牌,因为 ranges::shuffle 无法直接应用于 std::list。
#include <algorithm> #include <functional> #include <iostream> #include <list> #include <numeric> #include <random> #include <vector> template <typename Proj = std::identity> inline void print(auto comment, std::ranges::input_range auto&& range, Proj proj = {}) { for (std::cout << comment; auto const &element : range) std::cout << proj(element) << ' '; std::cout << '\n'; } int main() { std::list<int> list(8); // Fill the list with ascending values: 0, 1, 2, ..., 7 std::ranges::iota(list, 0); print("Contents of the list: ", list); // A vector of iterators (see the comment to Example) std::vector<std::list<int>::iterator> vec(list.size()); // Fill with iterators to consecutive list's elements std::ranges::iota(vec.begin(), vec.end(), list.begin()); std::ranges::shuffle(vec, std::mt19937 {std::random_device {}()}); print("Contents of the list viewed via vector: ", vec, [](auto it) { return *it; }); }
可能的输出:
Contents of the list: 0 1 2 3 4 5 6 7 Contents of the list viewed via vector: 5 7 6 0 1 3 4 2
参阅
将一个给定值复制赋值给一个范围内的每个元素 (函数模板) |
|
(C++20)
|
将一个给定值复制赋值给一个范围内的每个元素 (niebloid) |
将相继的函数调用结果赋值给一个范围中的每个元素 (函数模板) |
|
(C++20)
|
保存函数结果到一个范围中 (niebloid) |
(C++20)
|
由通过重复对某个初值自增所生成的序列组成的 view (类模板) (定制点对象) |
(C++11)
|
用从起始值开始连续递增的值填充一个范围 (函数模板) |