首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::iota

Defined in header <numeric>

?

?

template< class ForwardIterator, class T > void iota( ForwardIterator first, ForwardIterator last, T value );

?

(since C++11)

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

等效操作:

二次

代码语言:javascript
复制
*(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用于填充两个容器。

二次

代码语言:javascript
复制
#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';
}

二次

可能的产出:

二次

代码语言:javascript
复制
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

二次

另见

fill

copy-assigns the given value to every element in a range (function template)

generate

assigns the results of successive function calls to every element in a range (function template)

代码语言:txt
复制
 ? cppreference.com

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com