在线文档教程
C++
算法 | Algorithm

std::iota

STD::IATA

Defined in header
template< class ForwardIterator, class T > void iota( ForwardIterator first, ForwardIterator last, T value (since C++11)

填充范围[first, last)value++value...

等效操作:

二次

*(d_first) = value; *(d_first+1) = ++value; *(d_first+2) = ++value; *(d_first+3) = ++value; ...

二次

参数

first, last-the range of elements to fill with sequentially increasing values starting with value
value-initial value to store, the expression ++value must be well-formed

返回值

%280%29

复杂性

一点儿没错last - first

可能的实施

%2A第一++=值;++值;}

*。

注记

std::shuffle的向量std::list迭代器std::shuffle不能应用于std::list直接。std::iota用于填充两个容器。

二次

#include <algorithm> #include <iostream> #include <list> #include <numeric> #include <random> #include <vector> 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::shuffle(v.begin(), v.end(), std::mt19937{std::random_device{}()} 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'; }

二次

可能的产出:

二次

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

二次

另见

fillcopy-assigns the given value to every element in a range (function template)
generateassigns the results of successive function calls to every element in a range (function template)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。