std:: iota
| 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
<
class
ForwardIt,
class
T
>
void iota ( ForwardIt first, ForwardIt last, T value ) ; |
(C++11 起)
(C++20 起为 constexpr) |
|
用从
value
开始的值,并通过重复执行
++
value
依次递增,填充范围
[
first
,
last
)
。
等效操作(假设 ++ value 返回递增后的值):
*first = value; *++first = ++value; *++first = ++value; *++first = ++value; // 重复执行直到抵达“last”
若满足以下任一条件,则程序非良构:
-
T不可转换为ForwardIt的 值类型 。 -
表达式
++
val
格式错误,其中
val
是类型为
T的变量。
目录 |
参数
| first, last | - | 定义元素范围的迭代器对,该范围将以从 value 开始按顺序递增的值进行填充 |
| value | - | 存储的初始值 |
复杂度
恰好 std:: distance ( first, last ) 次递增和赋值操作。
可能的实现
template<class ForwardIt, class T> constexpr // 自 C++20 起 void iota(ForwardIt first, ForwardIt last, T value) { for (; first != last; ++first, ++value) *first = value; } |
注释
该函数得名于编程语言 APL 中的整数函数 ⍳ 。它是 STL 组件 中未被纳入 C++98 标准的部分之一,但最终在 C++11 中进入了标准库。
示例
以下示例将
std::shuffle
应用于
std::list
迭代器的
vector
。
std::iota
用于填充容器。
#include <algorithm> #include <iomanip> #include <iostream> #include <list> #include <numeric> #include <random> #include <vector> class BigData // inefficient to copy { int data[1024]; /* some raw data */ public: explicit BigData(int i = 0) { data[0] = i; /* ... */ } operator int() const { return data[0]; } BigData& operator=(int i) { data[0] = i; return *this; } /* ... */ }; int main() { std::list<BigData> l(10); std::iota(l.begin(), l.end(), -4); std::vector<std::list<BigData>::iterator> v(l.size()); std::iota(v.begin(), v.end(), l.begin()); // Vector of iterators (to original data) is used to avoid expensive copying, // and because std::shuffle (below) cannot be applied to a std::list directly. std::shuffle(v.begin(), v.end(), std::mt19937{std::random_device{}()}); std::cout << "Original contents of the list l:\t"; for (const auto& n : l) std::cout << std::setw(2) << n << ' '; std::cout << '\n'; std::cout << "Contents of l, viewed via shuffled v:\t"; for (const auto i : v) std::cout << std::setw(2) << *i << ' '; std::cout << '\n'; }
可能的输出:
Original contents of the list l: -4 -3 -2 -1 0 1 2 3 4 5 Contents of l, viewed via shuffled v: -1 5 -4 0 2 1 4 -2 3 -3
参见
|
(C++23)
|
用起始值的连续增量填充范围
(算法函数对象) |
|
将给定值复制赋值给范围内的每个元素
(函数模板) |
|
|
(C++20)
|
为范围内的元素分配特定值
(算法函数对象) |
|
将连续函数调用的结果赋值给范围内的每个元素
(函数模板) |
|
|
(C++20)
|
将函数结果保存到范围中
(算法函数对象) |
|
(C++20)
|
由重复递增初始值生成的序列组成的
view
(类模板) (定制点对象) |
将适配序列的每个元素映射到包含元素位置和值的元组的
view
(类模板) (范围适配器对象) |