非变异算法之计数(count, count_if)
2011-04-26 11:36
357 查看
主要函数
count, count_if.
统计数组中某个元素出现的次数及大于4的元素个数
count,count_if 应用于类
count, count_if.
template<class InIt, class T> size_t count(InIt first, InIt last, const T& val); template<class InIt, class Pred> size_t count_if(InIt first, InIt last, Pred pr);
统计数组中某个元素出现的次数及大于4的元素个数
#include <iostream> #include <vector> #include <iterator> #include <algorithm> using namespace std; bool matchExpress(int src) { return src > 4; } int main(int argc, char *argv[]) { vector<int>v; for (int i = 1; i <= 10; i++) { int temp; cin >> temp; v.push_back(temp); } cout << "/n输入要统计的数字: "; int theOne; cin >> theOne; int nCount = count(v.begin(), v.end(), theOne ); cout << theOne << " in total is " << nCount << endl << endl; int nSize = count_if(v.begin(), v.end(),matchExpress); cout << "> 4 的数字共有: " << nSize << endl; system("pause"); return 0; }
count,count_if 应用于类
#include <iostream> #include <vector> #include <algorithm> #include <cstdlib> using namespace std; class Student { private: string stuName; int stuNumber; int stuGrade; public: Student(const int number = 0, const string name = "none", const int grade = 0); const int number()const { return stuNumber; } const string name() const { return stuName; } const int grade() const { return stuGrade; } bool operator ==( const int grade) const { return stuGrade == grade; } }; Student::Student(const int number, const string name, const int grade) { stuNumber = number; stuName = name; stuGrade = grade; } class matchExpress { private: int grade; public: matchExpress(const int grade) { this->grade = grade; } bool operator()(const Student &stu) const { return stu.grade() == grade; } }; bool match(Student &stu) { return stu.grade() == 80; } int main(int argc, char *argv[]) { Student stu1(10001, "张三",70); Student stu2(10002, "李四",80); Student stu3(10003, "王码",90); vector<Student> stuTable; stuTable.push_back(stu1); stuTable.push_back(stu2); stuTable.push_back(stu3); int nCount1 = count(stuTable.begin(), stuTable.end(), 80); int nCount2 = count_if(stuTable.begin(), stuTable.end(), matchExpress(80)); int nCount3 = count_if(stuTable.begin(), stuTable.end(), match); cout << "分数为80的学生个数为: " << nCount1 << endl; cout << "分数为80的学生个数为: " << nCount2 << endl; cout << "分数为80的学生个数为: " << nCount3 << endl; system("pause"); return 0; }
相关文章推荐
- STL_算法_元素计数(count、count_if)
- C++语法基础--泛型算法(generic algorithm)--只读算法accumulate(),find_first_of(),count(),count_if()
- STL 统计vector容器中指定对象元素出现的次数:count()与count_if()算法
- STL算法之count. count_if
- STL常用算法: distance实现,count_if算法实现,bind2nd的使用..
- 变异算法之删除(remove, remove_if, remove_copy, remove_copy)
- MapReduce算法一、简单求和计数(类似WordCount)
- STL算法find,find_if,find_if_not,sort,fill,for_each,count,adjacent_find,random_shuffle,prev_permutation
- C++ STL 算法库学习之adjacent_difference,count,partial_sum,accumulate,count_if详解
- 非修改性序列算法之count和count_if
- 算法之旅,直奔<algorithm>之十 count_if
- 非变异算法之查询( find(), find_if(), find_first_of, adjacent_find(), find_end(), search(), search_n() )
- 【STL】非变异算法之计数
- 【LeetCode-面试算法经典-Java实现】【038-Count and Say(计数和表述)】
- 算法之旅,直奔<algorithm>之十 count_if
- 【每日一练】(第3期)关于STL的泛型算法count_if()
- 变异算法之替换(replace, replace_if, replace_copy, replace_copy_if)
- STL 统计vector容器中指定对象元素出现的次数:count()与count_if()算法
- 【BZOJ-1833】count数字计数 数位DP
- [bzoj 1833] [ZJOI2010]count 数字计数:数位DP