Difference between revisions of "CPP/STL/RandomData"
From ProgrammingExamples
< CPP
Daviddoria (Talk | contribs) (Created page with '==Generate.cpp== <source lang="cpp"> #include <iostream> #include <vector> #include <algorithm> template <typename T> class data_filler { public: data_filler(){} T operat…') |
Daviddoria (Talk | contribs) |
||
| (One intermediate revision by one other user not shown) | |||
| Line 5: | Line 5: | ||
#include <algorithm> | #include <algorithm> | ||
| − | + | struct RandFloat{ | |
| − | + | float operator()() { return rand()/(float)RAND_MAX; } | |
| − | { | + | |
| − | + | ||
| − | + | ||
| − | + | ||
}; | }; | ||
| Line 16: | Line 12: | ||
{ | { | ||
//create some random data | //create some random data | ||
| − | std::vector< | + | std::vector<float> data(10,0.0f); |
| − | std::generate(data.begin(), data.end(), | + | //from data.begin to end, generate random floats for each index |
| + | std::generate(data.begin(), data.end(), RandFloat()); | ||
| − | for(unsigned int i = 0; i < data.size(); i++) | + | for(unsigned int i = 0; i < data.size(); i++){ |
| − | + | ||
std::cout << data[i] << std::endl; | std::cout << data[i] << std::endl; | ||
} | } | ||
| Line 26: | Line 22: | ||
return 0; | return 0; | ||
} | } | ||
| + | |||
| + | </source> | ||
| + | |||
| + | ==CMakeLists.txt== | ||
| + | <source lang="cmake"> | ||
| + | cmake_minimum_required(VERSION 2.6) | ||
| + | |||
| + | PROJECT(Generate) | ||
| + | |||
| + | ADD_EXECUTABLE(Generate Generate.cpp) | ||
</source> | </source> | ||
Latest revision as of 08:24, 26 February 2011
Generate.cpp
#include <iostream> #include <vector> #include <algorithm> struct RandFloat{ float operator()() { return rand()/(float)RAND_MAX; } }; int main (int argc, char *argv[]) { //create some random data std::vector<float> data(10,0.0f); //from data.begin to end, generate random floats for each index std::generate(data.begin(), data.end(), RandFloat()); for(unsigned int i = 0; i < data.size(); i++){ std::cout << data[i] << std::endl; } return 0; }
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) PROJECT(Generate) ADD_EXECUTABLE(Generate Generate.cpp)