count_if.cpp 920 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Range v3 library
  2. //
  3. // Copyright Jeff Garland 2017
  4. //
  5. // Use, modification and distribution is subject to the
  6. // Boost Software License, Version 1.0. (See accompanying
  7. // file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // Project home: https://github.com/ericniebler/range-v3
  11. //
  12. ///[count_if]
  13. // This example counts element of a range that match a supplied predicate.
  14. // output
  15. // vector: 2
  16. // array: 2
  17. #include <array>
  18. #include <iostream>
  19. #include <range/v3/algorithm/count_if.hpp> // specific includes
  20. #include <vector>
  21. using std::cout;
  22. auto is_six = [](int i) -> bool { return i == 6; };
  23. int
  24. main()
  25. {
  26. std::vector<int> v{6, 2, 3, 4, 5, 6};
  27. auto c = ranges::count_if(v, is_six);
  28. cout << "vector: " << c << '\n'; // 2
  29. std::array<int, 6> a{6, 2, 3, 4, 5, 6};
  30. c = ranges::count_if(a, is_six);
  31. cout << "array: " << c << '\n'; // 2
  32. }
  33. ///[count_if]