代码实践
-
main.cpp
#include <iostream> using namespace std; #include "LessThan.h" #include <vector> int main() { int a[] = { 34,5,234,65,32,756,2,34 }; vector<int> ivec(a, a + sizeof(a) / sizeof(int)); cout << count_less_than(ivec, 100) << endl; print_less_than(ivec, 100); cout << endl; system("pause"); return 0; }
-
LessThan.h
#pragma once #include <vector> #include <iostream> #include <algorithm> using namespace std; class LessThan { public: LessThan(int val) //设置一个基值 :_val(val) {} bool operator()(int) const; private: int _val; }; int count_less_than(const vector<int>& ivec, int comp); void print_less_than(const vector<int>& ivec, int comp);
-
LessThan.cpp
#include "LessThan.h" inline bool LessThan::operator()(int val) const { return (val < this->_val); } int count_less_than(const vector<int>& ivec, int comp) { int count = 0; LessThan lt(comp); for (int i = 0; i < ivec.size(); i++) if (lt(ivec[i])) ++count; return count; } void print_less_than(const vector<int>& ivec, int comp) { vector<int>::const_iterator iter = ivec.begin(); vector<int>::const_iterator it_end = ivec.end(); LessThan lt(comp); while ((iter = find_if(iter, it_end, lt)) != it_end) { cout << *iter << " "; ++iter; } }
今天是20200326 慕课好像看不完了😭