Skip to content

Instantly share code, notes, and snippets.

@alchem0x2A
Created April 17, 2018 16:37
Show Gist options
  • Save alchem0x2A/938c3ae03ec1653476c749120e52ba5a to your computer and use it in GitHub Desktop.
Save alchem0x2A/938c3ae03ec1653476c749120e52ba5a to your computer and use it in GitHub Desktop.
[c++] Fill an array with lambda function
#include <algorithm>
#include <iostream>
#include <random>
int main() {
std::mt19937_64 gen(42);
std::uniform_real_distribution<double> uni(-10.0, 10.0);
double *numbers = new double[20];
/*use std::generate to fill the array pointer with lambda function*/
std::generate(numbers, &numbers[20],
[&]() { return uni(gen); });
for (int i=0; i < 20; ++i)
std::cout << numbers[i] << std::endl;
delete numbers;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment