C++ STL iota 函数说明

2019-09-26  本文已影响0人  book_02

说明

iota用一个从value递增的数列给[first, last)的容器赋值
等效于

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

C++11 才引入,之前版本没有此函数

头文件

#include <numeric>

名称来源

itoa 是 希腊语的第九个字母,读[aɪ'otə]
这个名称是从APL编程语言借鉴过来的。

For example, the integer function denoted by ι produces a vector of the first n integers when applied to the argument n, …

例子


#include <iostream>
#include <vector>
#include <numeric>

int main()
{
    std::vector<int> nums(10);

    for (int i : nums) {
        std::cout << i << "\t";
    }
    std::cout << std::endl;

    std::iota(nums.begin(), nums.end(), 5); // fill nums with 5, 6, 7, 8...

    for (int i : nums) {
        std::cout << i << "\t";
    }
    std::cout << std::endl;

    system("pause");
    return 0;
}

结果:

0       0       0       0       0       0       0       0       0       0
5       6       7       8       9       10      11      12      13      14

参考

http://www.cplusplus.com/reference/numeric/iota/
https://stackoverflow.com/questions/9244879/what-does-iota-of-stdiota-stand-for

上一篇 下一篇

猜你喜欢

热点阅读