fork download
  1. //C++11 required
  2.  
  3. #include <iostream>
  4. #include <iterator>
  5. #include <vector>
  6. #include <random>
  7. #include <algorithm>
  8.  
  9. class wind {
  10. public:
  11. //bitfields
  12. enum dirType {first,north=first,east,south,west,last=west} dir : 2;
  13. unsigned strength : 14;
  14. char getName() const {
  15. const static char windName[dirType::last+1] {'n','e','s','w'};
  16. return windName[dir];
  17. }
  18. };
  19.  
  20. std::ostream& operator<<(std::ostream& os, const wind& w) {
  21. return os << '{' << w.getName() << ',' << w.strength << '}';
  22. }
  23.  
  24. int main() {
  25. std::vector<wind> v(12);
  26.  
  27. std::mt19937 gen { std::random_device()() };
  28. std::uniform_int_distribution<int> rand_d(wind::dirType::first, wind::dirType::last+1);
  29. std::uniform_int_distribution<int> rand_s(0,100);
  30.  
  31. std::generate_n(v.begin(), v.size(), [&] {
  32. return wind {static_cast<wind::dirType>(rand_d(gen)), rand_s(gen)};
  33. });
  34.  
  35. std::copy(v.begin(), v.end(), std::ostream_iterator<wind>(std::cout, " "));
  36.  
  37. size_t count = std::count_if(v.begin(), v.end(), [](const wind& item) {
  38. return item.dir==wind::dirType::south && item.strength > 8;
  39. });
  40. std::cout << "\n----\ntotal ( south, more than 8 )\t: " << count << std::endl;
  41. }
Success #stdin #stdout 0s 4396KB
stdin
Standard input is empty
stdout
{s,53} {e,99} {n,41} {n,82} {s,25} {n,29} {s,38} {s,19} {e,89} {n,3} {n,69} {e,89} 
----
total ( south, more than 8 )	: 4