这两个方法都可用于统计容器中符合要求的元素的个数,直接上代码


       

bool IsGreaterEx(int x)
{
return x>3?true:false;
}
int main()
{
vector<int> vecInt;
vecInt.push_back(1);
vecInt.push_back(2);
vecInt.push_back(3);
vecInt.push_back(4);
vecInt.push_back(5);
cout<<count(vecInt.begin(),vecInt.end(),4)<<endl;
cout<<count_if(vecInt.begin(),vecInt.end(),IsGreaterEx)<<endl;
return 1;
}



调用count函数时,统计容器中等于4的元素的个数

调用count_if时,统计容器中大于3的元素的个数