A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/language/../language/../experimental/../algorithm/count.html below:

std::count, std::count_if - cppreference.com

(1) template< class InputIt, class T >

typename std::iterator_traits<InputIt>::difference_type

    count( InputIt first, InputIt last, const T& value );
(constexpr since C++20)
(until C++26) template< class InputIt, class T = typename std::iterator_traits

                                       <InputIt>::value_type >
constexpr typename std::iterator_traits<InputIt>::difference_type

    count( InputIt first, InputIt last, const T& value );
(since C++26) (2) template< class ExecutionPolicy, class ForwardIt, class T >

typename std::iterator_traits<ForwardIt>::difference_type
    count( ExecutionPolicy&& policy,

           ForwardIt first, ForwardIt last, const T& value );
(since C++17)
(until C++26) template< class ExecutionPolicy,

          class ForwardIt, class T = typename std::iterator_traits
                                         <ForwardIt>::value_type >
typename std::iterator_traits<ForwardIt>::difference_type
    count( ExecutionPolicy&& policy,

           ForwardIt first, ForwardIt last, const T& value );
(since C++26) template< class InputIt, class UnaryPred >

typename std::iterator_traits<InputIt>::difference_type

    count_if( InputIt first, InputIt last, UnaryPred p );
(3) (constexpr since C++20) template< class ExecutionPolicy, class ForwardIt, class UnaryPred >

typename std::iterator_traits<ForwardIt>::difference_type
    count_if( ExecutionPolicy&& policy,

              ForwardIt first, ForwardIt last, UnaryPred p );
(4) (since C++17)

Returns the number of elements in the range [firstlast) satisfying specific criteria.

1) Counts the elements that are equal to value (using operator==).

3) Counts elements for which predicate p returns true.

2,4) Same as (1,3), but executed according to policy.

These overloads participate in overload resolution only if all following conditions are satisfied:

[edit] Parameters first, last - the pair of iterators defining the range of elements to examine value - the value to search for policy - the execution policy to use p - unary predicate which returns ​true for the required elements.

The expression p(v) must be convertible to bool for every argument v of type (possibly const) VT, where VT is the value type of InputIt, regardless of value category, and must not modify v. Thus, a parameter type of VT&is not allowed, nor is VT unless for VT a move is equivalent to a copy(since C++11). ​

Type requirements -InputIt must meet the requirements of LegacyInputIterator. -ForwardIt must meet the requirements of LegacyForwardIterator. -UnaryPred must meet the requirements of Predicate. [edit] Return value

The number of iterators it in the range [firstlast) satisfying the following condition:

1,2) *it == value is true.

3,4) p(*it) != false is true.

[edit] Complexity

Given \(\scriptsize N\)N as std::distance(first, last):

1,2) Exactly \(\scriptsize N\)N comparisons with value using operator==.

3,4) Exactly \(\scriptsize N\)N applications of the predicate p.

[edit] Exceptions

The overloads with a template parameter named ExecutionPolicy report errors as follows:

[edit] Notes

For the number of elements in the range [firstlast) without any additional criteria, see std::distance.

[edit] Possible implementation

See also the implementations of count in libstdc++ and libc++.

See also the implementations of count_if in libstdc++ and libc++.

count (1)
template<class InputIt, class T = typename std::iterator_traits<InputIt>::value_type>
typename std::iterator_traits<InputIt>::difference_type
    count(InputIt first, InputIt last, const T& value)
{
    typename std::iterator_traits<InputIt>::difference_type ret = 0;
    for (; first != last; ++first)
        if (*first == value)
            ++ret;
    return ret;
}
count_if (3)
template<class InputIt, class UnaryPred>
typename std::iterator_traits<InputIt>::difference_type
    count_if(InputIt first, InputIt last, UnaryPred p)
{
    typename std::iterator_traits<InputIt>::difference_type ret = 0;
    for (; first != last; ++first)
        if (p(*first))
            ++ret;
    return ret;
}
[edit] Example
#include <algorithm>
#include <array>
#include <cassert>
#include <complex>
#include <iostream>
#include <iterator>
 
int main()
{
    constexpr std::array v{1, 2, 3, 4, 4, 3, 7, 8, 9, 10};
    std::cout << "v: ";
    std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
 
    // Determine how many integers match a target value.
    for (const int target : {3, 4, 5})
    {
        const int num_items = std::count(v.cbegin(), v.cend(), target);
        std::cout << "number: " << target << ", count: " << num_items << '\n';
    }
 
    // Use a lambda expression to count elements divisible by 4.
    int count_div4 = std::count_if(v.begin(), v.end(), [](int i) { return i % 4 == 0; });
    std::cout << "numbers divisible by four: " << count_div4 << '\n';
 
    // A simplified version of `distance` with O(N) complexity:
    auto distance = [](auto first, auto last)
    {
        return std::count_if(first, last, [](auto) { return true; });
    };
    static_assert(distance(v.begin(), v.end()) == 10);
 
    std::array<std::complex<double>, 3> nums{{{4, 2}, {1, 3}, {4, 2}}};
    #ifdef __cpp_lib_algorithm_default_value_type
        // T gets deduced making list-initialization possible
        auto c = std::count(nums.cbegin(), nums.cend(), {4, 2});
    #else
        auto c = std::count(nums.cbegin(), nums.cend(), std::complex<double>{4, 2});
    #endif
    assert(c == 2);
}

Output:

v: 1 2 3 4 4 3 7 8 9 10
number: 3, count: 2
number: 4, count: 2
number: 5, count: 0
numbers divisible by four: 3
[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior LWG 283 C++98 T was required to be EqualityComparable, but
the value type of InputIt is not always T removed the requirement [edit] See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4