fork download
  1. #include <unordered_map>
  2. #include <random>
  3. #include <iostream>
  4.  
  5. template <typename Numeric, typename Divisor>
  6. inline constexpr bool isDivisibleBy(Numeric numeric, Divisor divisor)
  7. {
  8. return ((numeric >= divisor) && ((numeric % divisor) == 0));
  9. }
  10.  
  11. constexpr auto isEven = [](int test) -> bool { return isDivisibleBy<int, int>(test, 2); };
  12.  
  13. int main()
  14. {
  15. std::mt19937 randomEngine{std::random_device{}()};
  16. std::uniform_int_distribution<int> randomDistribution{0, 100};
  17. std::unordered_map<int, bool> hash{};
  18. for (int i = 0; i < 10; i++) {
  19. int randomNumber{randomDistribution(randomEngine)};
  20. hash.emplace(randomNumber, isEven(randomNumber));
  21. }
  22. for (auto &hash : hash) {
  23. std::cout << "key \"" << hash.first << "\" is " << (hash.second ? "even" : "odd") << std::endl;
  24. }
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
key "25" is odd
key "35" is odd
key "86" is even
key "97" is odd
key "53" is odd
key "12" is even
key "19" is odd
key "52" is even
key "41" is odd
key "73" is odd