返回元素数。值满足指定条件的大小。
template<class InputIterator, class Predicate>
typename iterator_traits<InputIterator>::difference_type count_if(
InputIterator _First,
InputIterator _Last,
Predicate _Pred
);
参数
_First
解决输入的迭代器第一个元素的位置在要搜索的范围。_Last
解决输入的迭代器通过最终元素的位置一在要搜索的范围。_Pred
定义要满足条件的用户定义的谓词函数对象,如果元素将计数。谓词采用单个参数并返回 true 或 false。
返回值
满足条件元素的数量谓词所指定的。
备注
此模板函数是算法 计数的泛化,替换谓词“等于一个特定值”的所有谓词。
示例
// alg_count_if.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <iostream>
bool greater10(int value)
{
return value >10;
}
int main()
{
using namespace std;
vector<int> v1;
vector<int>::iterator Iter;
v1.push_back(10);
v1.push_back(20);
v1.push_back(10);
v1.push_back(40);
v1.push_back(10);
cout << "v1 = ( ";
for (Iter = v1.begin(); Iter != v1.end(); Iter++)
cout << *Iter << " ";
cout << ")" << endl;
vector<int>::iterator::difference_type result1;
result1 = count_if(v1.begin(), v1.end(), greater10);
cout << "The number of elements in v1 greater than 10 is: "
<< result1 << "." << endl;
}
要求
标头: <algorithm>
命名空间: std