#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
vector <int> a;
a.push_back( 1);
a.push_back( 1);
a.push_back( 2);
a.push_back( 2);
a.push_back( 3);
a.push_back( 3);
int count1, count2;
// 1 count( iterator 1, iterator 2, key)
count1 = count( a.begin(), a.end(), 1);
cout << "number of 1 is " << count1 << endl;
// 2 count_if( iterator 1, iterator 2, 一元谓词)
count2 = count_if( a.begin(), a.end(), [](int t){
return ( t % 2 == 0);
});
cout << "number of even elements is " << count2 << endl;
}
C++ count count_if
最新推荐文章于 2023-10-17 14:09:02 发布