Created
          April 17, 2018 16:37 
        
      - 
      
- 
        Save alchem0x2A/938c3ae03ec1653476c749120e52ba5a to your computer and use it in GitHub Desktop. 
    [c++] Fill an array with lambda function
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #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