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
TODOC++23
TODOC++20
TODOC++17
TODOC++14
TODOC++11
TODOSee also
TODO
This page was last modified on 2024-02-16