tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
count.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2020 Tobias Hienzsch
3
4#ifndef TETL_ALGORITHM_COUNT_HPP
5#define TETL_ALGORITHM_COUNT_HPP
6
7#include <etl/_iterator/iterator_traits.hpp>
8
9namespace etl {
10
11/// \brief Returns the number of elements in the range `[first, last)`
12/// satisfying specific criteria. Counts the elements that are equal to value.
13///
14/// \param first The range of elements to examine.
15/// \param last The range of elements to examine.
16/// \param value The value to search for.
17///
18/// https://en.cppreference.com/w/cpp/algorithm/count
19///
20/// \ingroup algorithm
21template <typename InputIt, typename T>
22[[nodiscard]] constexpr auto count(InputIt first, InputIt last, T const& value) ->
23 typename iterator_traits<InputIt>::difference_type
24{
25 auto result = typename etl::iterator_traits<InputIt>::difference_type{0};
26 for (; first != last; ++first) {
27 if (*first == value) {
28 ++result;
29 }
30 }
31 return result;
32}
33
34} // namespace etl
35
36#endif // TETL_ALGORITHM_COUNT_HPP
constexpr auto count(InputIt first, InputIt last, T const &value) -> typename iterator_traits< InputIt >::difference_type
Returns the number of elements in the range [first, last) satisfying specific criteria....
Definition count.hpp:22
Definition adjacent_find.hpp:9
iterator_traits is the type trait class that provides uniform interface to the properties of LegacyIt...
Definition iterator_traits.hpp:48