template |
|
Return number of elements in range satisfying condition
Returns the number of elements in the range [first,last) for which condition pred is true.
The behavior of this function template is equivalent to:
template <class InputIterator, class Predicate> ptrdiff_t count_if ( InputIterator first, InputIterator last, Predicate pred ) { ptrdiff_t ret=0; while (first != last) if (pred(*first++)) ++ret; return ret; } |
The return type (iterator_traits
#include
#include
#include
using namespace std;;
class Student{
public:
int No;
string strName;
int grade;
Student(int No,string strName,int grade):No(No),strName(strName),grade(grade){};
/*bool operator ==(int grade){
return this->grade==grade;
}*/
};
class MatchExpress{
int grade;
public:
MatchExpress(int grade):grade(grade){};
bool operator()(Student& s){
return s.grade>grade;
}
};
int main(){
vectorv;
Student s1(1000,"张三",80);
Student s2(1001,"李四",85);
Student s3(1002,"王五",80);
Student s4(1003,"赵六",80);
v.push_back(s1);
v.push_back(s2);
v.push_back(s3);
v.push_back(s4);
int nCount;
nCount=count_if(v.begin(),v.end(),MatchExpress(80));
cout<