c++11:iota

iota:

Fills the range [first, last) with sequentially(循环的) increasing values, starting with value and repetitively(重复地) evaluating ++value.

Parameters

  first, last:the range of elemets to fill with sequentially increasing values starting with value

 

  vaule: intial value to store, the expression ++value must be well-formed  

Return value

(none)

 

Possible implementation

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

我的例子:

  1 #include <numeric>
  2 #include <vector>
  3 #include <iostream>
  4 using namespace std;
  5 
  6 int main()
  7 {
  8     int a[5];
  9     iota(a, a+5, 10);
 10     for (auto &r: a)
 11         cout << r ;
 12     cout << endl;
 13 
 14     vector<int> va(5);
 15     iota(va.begin(), va.end(), 4);
 16     for (auto &v : va)
 17         cout << v << " ";
 18     cout << endl;
 19 
 20     vector<vector<int>::iterator> vb(va.size());
 21     itoa(vb.begin(), vb.end(), va.begin());
 22     for (auto &v : vb)
 23         cout << *v << " ";
 24     cout << endl;
 25 }

输出结果:

  10,11, 12, 13, 14 

  10,11, 12, 13, 14 

  10,11, 12, 13, 14 

原文地址:https://www.cnblogs.com/457220157-FTD/p/4021902.html