C/C++:iota()函數

iota() 函數
定義在 頭文件numeric 中
#include <numeric>

函數模板:

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

前兩個參數是定義序列的正向迭代器,保存序列初始以及結尾的位置 [first, last)
第三個參數是累加器的初始 T 值。

函數模板內的具體操作:

template <class ForwardIterator, class T>
  void iota (ForwardIterator first, ForwardIterator last, T val)
{
  while (first!=last) {
    *first = val;
    ++first;
    ++val;
  }
}

用法舉例:

// iota example
#include <iostream>     // std::cout
#include <numeric>      // std::iota

int main () {
  int numbers[10];

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

  std::cout << "numbers:";
  for (int& i:numbers) std::cout << ' ' << i;
  std::cout << '\n';

  return 0;
}

Output:

numbers: 100 101 102 103 104 105 106 107 108 109

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章