count.cpp 965 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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]
  13. // This example demonstrates counting the number of
  14. // elements that match a given value.
  15. // output...
  16. // vector: 2
  17. // array: 2
  18. #include <iostream>
  19. #include <range/v3/algorithm/count.hpp> // specific includes
  20. #include <vector>
  21. using std::cout;
  22. int
  23. main()
  24. {
  25. std::vector<int> v{6, 2, 3, 4, 5, 6};
  26. // note the count return is a numeric type
  27. // like int or long -- auto below make sure
  28. // it matches the implementation
  29. auto c = ranges::count(v, 6);
  30. cout << "vector: " << c << '\n';
  31. std::array<int, 6> a{6, 2, 3, 4, 5, 6};
  32. c = ranges::count(a, 6);
  33. cout << "array: " << c << '\n';
  34. }
  35. ///[count]