an example of C++ STL algorithm generate using a lambda

initialized lambda captures are a C++14 extension



👍 g++ -std=c++14 generate.cpp
👍 ./a.out
3 3 3 3 3 
0 1 2 3 4 
👍 cat generate.cpp 
#include <iostream>
#include <vector>
using namespace std;

ostream& operator<<(ostream &o, 
                    vector<int> v){
  for(auto e : v)
    cout << e << ' ';
  return o;
}

int main() {
  vector<int> v(5, 0); 

  generate(v.begin(), v.end(),
    [](){return 3;});
  cout << v << endl;
  
  generate(v.begin(), v.end(),
    [i=0]()mutable{return i++;});
  cout << v << endl;
}