std::ranges:: iota, std::ranges:: iota_result
std::ranges
| Non-modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Partitioning operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Sorting operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Binary search operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Set operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Heap operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Minimum/maximum operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Permutation operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Fold operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Operations on uninitialized storage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Return types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Common mathematical functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Mathematical special functions (C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Mathematical constants (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Basic linear algebra algorithms (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Data-parallel types (SIMD) (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Floating-point environment (C++11) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Complex numbers | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Numeric array (
valarray
)
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Pseudo-random number generation | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Bit manipulation (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Saturation arithmetic (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Factor operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Interpolations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Generic numeric operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| C-style checked integer arithmetic | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<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 起) |
|
辅助类型
|
||
|
template
<
class
O,
class
T
>
using iota_result = ranges:: out_value_result < O, T > ; |
(3) | (C++23 起) |
用从
value
开始的值,并通过重复执行
++
value
依次递增,填充范围
[
first
,
last
)
。
等效操作:
*(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> void println(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); println("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 {}()}); println("List viewed via vector: ", vec, [](auto it) { return *it; }); }
可能的输出:
List: 0 1 2 3 4 5 6 7 List viewed via vector: 5 7 6 0 1 3 4 2
参见
|
将给定值复制赋值给范围内的每个元素
(函数模板) |
|
|
(C++20)
|
为范围内的元素赋予特定值
(算法函数对象) |
|
将连续函数调用的结果赋值给范围内的每个元素
(函数模板) |
|
|
(C++20)
|
将函数结果保存到范围中
(算法函数对象) |
|
(C++20)
|
由重复递增初始值生成的序列组成的
view
(类模板) (定制点对象) |
|
(C++11)
|
使用起始值的连续增量填充范围
(函数模板) |