@specsoftdev live:.cid.8e17e9b93cabb607 specsoftdev@gmail.com
std::count_if
Актуально для C++23.

#include <algorithm>
Актуально на 2024-02-16.


Define overload #1
template<class InputIterator, class Predicate>
constexpr typename iterator_traits<InputIterator>::difference_type
count_if(InputIterator first, InputIterator last, Predicate pred);

Подсчитывает количество элементов в диапазоне [first, last] для которых пользовательский предикат вернёт true.
Вернёт их количество.
Example, possible implementation
Define overload #2
template <class ExecutionPolicy, class ForwardIterator, class Predicate>
typename iterator_traits<ForwardIterator>::difference_type>
count_if(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, Predicate pred);

Example, possible implementation

Examples


Example 1:
#include <iostream>
#include <algorithm>
int main()
{
    auto list = {1, 2, 3, 1, 2, 3, 3};

    auto count3 = std::count_if(std::begin(list), std::end(list), [](auto s){return s == 3;});
    auto count7 = std::count_if(std::begin(list), std::end(list), [](auto s){return s == 7;});

    std::cout << "count3: " << count3 << std::endl;
    std::cout << "count7: " << count7 << std::endl;
    return 0;
}

count3: 3
count7: 0



Changelog

C++26
TODO
C++23
TODO
C++20
TODO
C++17
TODO
C++14
TODO
C++11
TODO


See also

TODO

This page was last modified on 2024-02-16