失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > c语言iota怎么用 C++ std::iota用法及代码示例

c语言iota怎么用 C++ std::iota用法及代码示例

时间:2018-07-28 02:37:18

相关推荐

c语言iota怎么用 C++ std::iota用法及代码示例

存储顺序递增

分配val的[first,last]个连续值范围内的每个元素,就像在写入每个元素之后以++ val递增。

模板:

void iota (ForwardIterator first, ForwardIterator last, T val);

参数:

first, last

Forward iterators to the initial and final positions of the sequence

to be written. The range used is [first, last), which contains all the

elements between first and last, including the element pointed by

first but not the element pointed by last.

val

Initial value for the accumulator.

返回类型:

None

// CPP program to illustrate

// std::iota

#include // std::cout

#include // std::iota

// Driver code

int main()

{

int numbers[10];

// Initailising starting value as 100

int st = 100;

std::iota(numbers, numbers + 10, st);

std::cout << "Elements are:";

for (auto i:numbers)

std::cout << ' ' << i;

std::cout << '\n';

return 0;

}

输出:

Elements are:100 101 102 103 104 105 106 107 108 109

应用:

它可用于生成连续的数字序列。

// CPP program to generate

// a sequence of numbers using std::iota

#include // std::cout

#include // std::iota

// Driver code

int main()

{

int numbers[11];

// Initailising starting value as 10

int st = 10;

std::iota(numbers, numbers + 11, st);

std::cout << "Elements are:";

for (auto i:numbers)

std::cout << ' ' << i;

std::cout << '\n';

return 0;

}

输出:

Elements are:10 11 12 13 14 15 16 17 18 19 20

如果觉得《c语言iota怎么用 C++ std::iota用法及代码示例》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。