在线文档教程
C++
算法 | Algorithm

std::count_if

STD:计数,std::count[医]如果

Defined in header
template< class InputIt, class T > typename iterator_traits<InputIt>::difference_type count( InputIt first, InputIt last, const T &value (1)
template< class ExecutionPolicy, class ForwardIt, class T > typename iterator_traits<ForwardIt>::difference_type count( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, const T &value (2)(since C++17)
template< class InputIt, class UnaryPredicate > typename iterator_traits<InputIt>::difference_type count_if( InputIt first, InputIt last, UnaryPredicate p (3)
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate > typename iterator_traits<ForwardIt>::difference_type count_if( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPredicate p (4)(since C++17)

返回范围中的元素数。[first, last)满足特定标准。

1%29计数等于value...

3%29计数谓词的元素p回报true...

2,4%29与%281,3%29相同,但根据policy。此重载只参与以下情况下的过载解决方案:std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

参数

first, last-the range of elements to examine
value-the value to search for
policy-the execution policy to use. See execution policy for details.
p-unary predicate which returns ​true for the required elements. The signature of the predicate function should be equivalent to the following: bool pred(const Type &a The signature does not need to have const &, but the function must not modify the objects passed to it. The type Type must be such that an object of type InputIt can be dereferenced and then implicitly converted to Type. ​

类型要求

-输入必须符合输入器的要求。

---。

返回值

满足条件的元素数。

复杂性

一点儿没错last---first谓词的比较/应用。

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。

  • 如果算法不能分配内存,std::bad_alloc被扔了。

注记

表示范围内的元素数。[first, last)没有任何附加标准,请参见std::distance...

可能的实施

第一版

*。

模板<class Inputit,class T>type Name迭代器[医]性状<InputIt>*差异[医]键入Count%28 InputIt First,InputIt Lest,Const T&value%29{type Name迭代器[医]性状<InputIt>*差异[医]类型RET=0;对于%28;第一%21=最后;++第29%(如果%28)%2A第一==值%29{ret++;}返回ret;}

第二版

模板<类Inputit,类UnaryPredicate>type Name迭代器[医]性状<InputIt>*差异[医]类型计数[医]如果%28 InputIt First,InputIt Lest,UnaryPredicate p%29{type Name迭代器[医]性状<InputIt>*差异[医]类型RET=0;对于%28;第一%21=最后;++First%29{if%28p%28%2A第一%29%29{ret++;}返回ret;}

二次

#include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 }; // determine how many integers in a std::vector match a target value. int target1 = 3; int target2 = 5; int num_items1 = std::count(v.begin(), v.end(), target1 int num_items2 = std::count(v.begin(), v.end(), target2 std::cout << "number: " << target1 << " count: " << num_items1 << '\n'; std::cout << "number: " << target2 << " count: " << num_items2 << '\n'; // use a lambda expression to count elements divisible by 3. int num_items3 = std::count_if(v.begin(), v.end(), [](int i){return i % 3 == 0;} std::cout << "number divisible by three: " << num_items3 << '\n'; }

二次

产出:

二次

number: 3 count: 2 number: 5 count: 0 number divisible by three: 3

二次

另见

二次

*。

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/Algorithm/count[医]如果