Skip to content

Instantly share code, notes, and snippets.

@lichray
Last active September 9, 2018 13:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lichray/64674ef6c4d57b5245be to your computer and use it in GitHub Desktop.
Save lichray/64674ef6c4d57b5245be to your computer and use it in GitHub Desktop.
generate_n is a better iota_n.
#include <algorithm>
#include <vector>
#include <iterator>
#include <iostream>
int main()
{
std::vector<int> v;
// instead of asking for iota_n(std::back_inserter(v), 10, 1);
std::generate_n(back_inserter(v), 10, [n = 0]() mutable { return ++n; });
for (auto i : v)
std::cout << i << ' ';
std::cout << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment