std::iota
来自cppreference.com
![]() |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Defined in header <numeric>
|
||
template< class ForwardIterator, class T > void iota( ForwardIterator first, ForwardIterator last, T value ); |
(C++11 起) | |
[first, last)
依次增加值填充的范围内,开始与value
和反复评估++value.Original:
Fills the range
[first, last)
with sequentially increasing values, starting with value
and repetitively evaluating ++value.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
相同的操作
Original:
Equivalent operation:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
*(d_first) = value; *(d_first+1) = ++value; *(d_first+2) = ++value; *(d_first+3) = ++value; ...
目录 |
[编辑] 参数
first, last | - | 元素的范围总和
Original: the range of elements to sum The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
value | - | 存储的初始值,则表达式+ +值必须很好地形成
Original: initial value to store, the expression ++value must be well-formed The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[编辑] 返回值
(无)
Original:
(none)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 复杂性
究竟
last - first
增量和任务.Original:
Exactly
last - first
increments and assignments.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 可能的实现
template<class ForwardIterator, class T> void iota(ForwardIterator first, ForwardIterator last, T value) { while(first != last) { *first++ = value; ++value; } } |
[编辑] 为例
以下示例适用于std::random_shuffle一个向量的迭代器到std::liststd::random_shuffle可以被应用到std::list。 std::iota被用于创建矢量.
Original:
The following example applies std::random_shuffle to a vector of iterators to a std::list since std::random_shuffle cannot be applied to an std::list directly. std::iota is used to create the vector.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#include <numeric> #include <algorithm> #include <list> #include <vector> #include <iostream> int main() { std::list<int> l(10); std::iota(l.begin(), l.end(), -4); std::vector<std::list<int>::iterator> v(l.size()); std::iota(v.begin(), v.end(), l.begin()); std::random_shuffle(v.begin(), v.end()); std::cout << "Contents of the list: "; for(auto n: l) { std::cout << n << ' '; } std::cout << '\n'; std::cout << "Contents of the list, shuffled: "; for(auto i: v) { std::cout << *i << ' '; } std::cout << '\n'; }
Output:
Contents of the list: -4 -3 -2 -1 0 1 2 3 4 5 Contents of the list, shuffled: 0 -1 3 4 -4 1 -2 -3 2 5
[编辑] 另请参阅
分配一个一定值范围内的元素 Original: assigns a range of elements a certain value The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) | |
并将结果保存在一个范围内的功能 Original: saves the result of a function in a range The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) |