TOC泛型算法和函数对象
//模拟实现拷贝 template <typename INSERT_IT, typename IT> void my_copy(const IT &first,const IT &last, INSERT_IT insert_it) { IT tmp = first; for (; tmp != last; tmp++) { *insert_it = *tmp; } } int main() { vector<int> v1; for (int i = 0; i <10; i++) { v1.push_back(i); } // 迭代器区间 拷贝到输出流迭代器 my_copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));//完成输出v1 cout << endl; vector<int> v2; my_copy(v1.begin(), v1.end(), inserter(v2, v2.begin())); my_copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));//完成输出v2 return 0;
template <typename I, typename IT> IT mfind(const IT& first, const IT &last, const I &val)//返回值为迭代器类型 { IT tmp = first; for (; tmp != last; tmp++) { if (*tmp == val) { return tmp; } } return last; } int main() { vector<int> v1; for (int i = 0; i <10; i++) { v1.push_back(i); } // 迭代器区间 拷贝到输出流迭代器 my_copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));//完成输出v1 cout << endl; vector<int>::iterator it=mfind(v1.begin(), v1.end(), 5); if (it == v1.end())//等于第二个参数,没找到 { cout << "not find" << endl; } else { cout << *it << endl; } return 0;
从小到大排序
template <typename T> void mswap(T&a, T&b) { T tmp = a; a = b; b = tmp; } template <typename IT> void msort(const IT& first, const IT& last) //用选择排序来实现 { for (IT tmp = first; tmp != last; tmp++) { for (IT tmp1 = tmp + 1; tmp1 != last; tmp1++) { if (!(*tmp < *tmp1)) //if (!pre(*tmp, *tmp1)) { mswap(*tmp, *tmp1); } } } } int main() { vector<int> v1; for (int i = 10; i >0; i--) { v1.push_back(i); } // 迭代器区间 拷贝到输出流迭代器 my_copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));//完成输出v1 cout << endl; //从小到大排序 msort(v1.begin(), v1.end()); my_copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
通过函数对象,实现从小到大(less)排序,
或从大到小(greater)排序
template <typename IT,typename PRE> void msort(const IT& first, const IT& last,PRE pre) { for (IT tmp = first; tmp != last; tmp++) { for (IT tmp1 = tmp + 1; tmp1 != last; tmp1++) { if(!pre(*tmp,*tmp1)) { mswap(*tmp, *tmp1); } } } }
第三个参数为Mless的对象
msort(v1.begin(), v1.end(),Mless());
查找第一个小于10的
template <typename IT,typename PRE> IT mfind_if(const IT& first, const IT& last, PRE pre)//传入绑定器的对象 { IT tmp = first; for (; tmp != last; tmp++) { //if (*tmp < 10) //if(less(*tmp,10)) //外界会将10传进来 拿到tmp和10进行比较,是否满足查找条件 if(pre(*tmp))//给pre对象传入遍历的tmp的值 看它是否满足 { return tmp; } } return last; } int main() { vector<int> v1; for (int i = 10; i >0; i--) { v1.push_back(i); } // 迭代器区间 拷贝到输出流迭代器 my_copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));//完成输出v1 cout << endl; //查找第一个小于10的元素 //绑定Mless对象的第二个参数 vector<int>::iterator it1=find_if(v1.begin(), v1.end(), binder2nd<Mless<int>>(Mless<int>(),10)); if (it1 == v1.end())//等于第二个参数,没找到 { cout << "not find" << endl; } else { cout << *it1 << endl; } return 0; }
//函数对象 template<typename T> class Mless { public: bool operator()(T a, T b)//重载括号运算符 { cout << "bool operator()(T a, T b)" << endl; return a < b; } }; template<typename T> bool mless(T a, T b) { return a < b; } int main() { vector<int> v1; for (int i = 10; i >0; i--) { v1.push_back(i); } // 迭代器区间 拷贝到输出流迭代器 my_copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));//完成输出v1 cout << endl; Mless<int>_less; _less(10, 20);
template<typename T> class Mgreater { public: bool operator()(T a, T b) { cout << "bool operator()(T a, T b)" << endl; return a > b; } };
//绑定器1 template<typename PRE> class Mbinder1st { public: Mbinder1st(PRE pre, typename PRE::TYPE_FIRST val) :_pre(pre), _val(val) { } typename PRE::TYPE_RET operator()(typename PRE::TYPE_SECOND tmp) //绑定器本身也是一个函数对象 //给函数对象的第一个参数绑定参数,其余参数由外界传入 { return _pre(_val,tmp); } private: PRE _pre; typename PRE::TYPE_FIRST _val; };
//函数对象 template<typename T> class Mless { public: typedef T TYPE_FIRST; typedef T TYPE_SECOND; typedef bool TYPE_RET//返回值类型 bool operator()(T a, T b) //重载括号运算符 { cout << "bool operator()(T a, T b)" << endl; return a < b; } }; //绑定器2 给函数参数绑定参数 template<typename PRE> class Mbinder2nd { public: Mbinder2nd(PRE pre,typename PRE::TYPE_SECOND val) :_pre(pre), _val(val) { } typename PRE::TYPE_RET operator()(typename PRE::TYPE_FIRST tmp) //绑定器本身也是一个函数对象 //给函数对象的第二个参数绑定参数,其余参数由外界传入 { return _pre(tmp, _val); } private: PRE _pre; typename PRE::TYPE_SECOND _val; };