1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <algorithm>
#include <iterator>
std::vector<int> wholeNumbers (int n) {
std::vector<int> v;
for (int i = 0; i < n; i++)
v.emplace_back (i);
return v;
}
template <typename...T>
void random_if_else_if (const std::vector<std::function<bool(T...)>>& predicate_and_action, T...t) {
static std::vector<int> sequence = wholeNumbers (predicate_and_action.size());
// initialize sequence just once for the entire program (and move constructor is called here)
std::random_shuffle (std::begin (sequence), std::end (sequence));
for (int pos : sequence) // range-based for-loop might be faster than pre-C++11 for-loop
if (predicate_and_action[pos](t...))
break;
}
int main() { // Example use of random_if_else_if
std::srand (std::time (nullptr));
const std::vector<std::function<bool(int, int, int, double, double, std::string)>> predicate_and_action = {
[](int a, int b, int c, double d, double e, std::string s)->bool {return (a + b + c + (int)std::round(d + e) + s.length()) % 2 ? std::cout << "odd\n" : false ;},
[](int a, int b, int c, double d, double e, std::string s)->bool {return (a + b + c + (int)std::round(d + e) + s.length()) % 2 == 0 ? std::cout << "even\n" : false ;},
[](int a, int b, int c, double d, double e, std::string s)->bool {return (a + b + c + (int)std::round(d + e) + s.length()) % 3 == 0 ? std::cout << "divisible by 3\n" : false ;},
[](int a, int b, int c, double d, double e, std::string s)->bool {return (a + b + c + (int)std::round(d + e) + s.length()) % 5 == 0 ? std::cout << "divisible by 5\n" : false ;},
[](int a, int b, int c, double d, double e, std::string s)->bool {return (a + b + c + (int)std::round(d + e) + s.length()) % 6 == 0 ? std::cout << "divisible by 6\n" : false ;},
[](int a, int b, int c, double d, double e, std::string s)->bool {return a + b + c + (int)std::round(d + e) + s.length() < 10 ? std::cout << "less than 10\n" : false ;},
[](int a, int b, int c, double d, double e, std::string s)->bool {return a + b + c + (int)std::round(d + e) + s.length() < 15 ? std::cout << "less than 15\n" : false ;},
[](int a, int b, int c, double d, double e, std::string s)->bool {return a + b + c + (int)std::round(d + e) + s.length() < 20 ? std::cout << "less than 20\n" : false ;},
[](int a, int b, int c, double d, double e, std::string s)->bool {return a + b + c + (int)std::round(d + e) + s.length() < 25 ? std::cout << "less than 25\n" : false ;},
[](int a, int b, int c, double d, double e, std::string s)->bool {return a + b + c + (int)std::round(d + e) + s.length() < 30 ? std::cout << "less than 30\n" : false ;}
};
const double PI = 3.14159;
const std::string JLBorges = "JLBorges";
for (int i = 0; i < 5; ++i)
for (int j = 0; j < 5; ++j)
for (int k = 0; k < 5; ++k)
{
std::cout << i + j + k + int(std::round (4.1+PI)) + JLBorges.length() << ' ';
random_if_else_if (predicate_and_action, i, j, k, 4.1, PI, JLBorges);
}
}
|