demo 二元函数对象
#include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; template <typename T> class SumVector { public: T operator()(T t1, T t2) // 二元函数对象 { return t1 + t2; } protected: private: }; void play01() { vector<int> v1, v2, v3; v1.push_back(1); v1.push_back(3); v1.push_back(5); v2.push_back(2); v2.push_back(4); v2.push_back(6); v3.resize(10); transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), SumVector<int>()); /* transform函数原型 template<class _InIt1, class _InIt2, class _OutIt, class _Fn2> inline _OutIt transform(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _OutIt _Dest, _Fn2 _Func) { // transform [_First1, _Last1) and [_First2, ...) with _Func _DEBUG_RANGE(_First1, _Last1); _DEBUG_POINTER(_Dest); _DEBUG_POINTER(_Func); if (_First1 != _Last1) return (_Transform2(_Unchecked(_First1), _Unchecked(_Last1), _First2, _Dest, _Func, _Is_checked(_Dest))); return (_Dest); } */ // transform把运算结果迭代器的开始位置返回出来 for (vector<int>::iterator it = v3.begin(); it != v3.end(); ++it) { cout << *it << ' '; } cout << endl; // 3 7 11 0 0 0 0 0 0 0 } int main() { play01(); return 0; }
#include <iostream> #include <cstdio> #include <vector> #include <algorithm> #include <ctime> using namespace std; void printVector(vector<int> &v) { for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) { cout << *it << ' '; } cout << endl; } void FuncShowElemt2(const int &t) { cout << t << ' '; } // 二元谓词 bool myCompare(const int &a, const int &b) { return a < b; } void play01() { vector<int> v(10); srand(time(0)); for (int i = 0; i < 10; i++) { int tmp = rand() % 100; v[i] = tmp; } printVector(v); // 90 19 94 50 90 90 24 50 30 74 for_each(v.begin(), v.end(), FuncShowElemt2); cout << endl; // 90 19 94 50 90 90 24 50 30 74 sort(v.begin(), v.end(), myCompare); for_each(v.begin(), v.end(), FuncShowElemt2); cout << endl; // 0 8 14 23 32 33 44 45 63 80 } int main() { play01(); return 0; }
#include <iostream> #include <cstdio> #include <set> #include <algorithm> #include <string> using namespace std; struct CompareNoCase { bool operator()(const string &str1, const string &str2) { string tmpstr1; tmpstr1.resize(str1.size()); transform(str1.begin(), str1.end(), tmpstr1.begin(), tolower); string tmpstr2; tmpstr2.resize(str2.size()); transform(str2.begin(), str2.end(), tmpstr2.begin(), tolower); return tmpstr1 < tmpstr2; } }; void play01() { set<string> set1; set1.insert("lucifer"); set1.insert("zhang"); set1.insert("yaoqi"); set<string>::iterator it1 = set1.find("LUcifer"); // find函数默认区分大小写 if (it1 == set1.end()) { cout << "find fail\n"; } else { cout << "find success\n"; } // find fail set<string, CompareNoCase> set2; set2.insert("lucifer"); set2.insert("zhang"); set2.insert("yaoqi"); set<string, CompareNoCase>::iterator it2 = set2.find("LUcifer"); // find函数默认区分大小写 if (it2 == set2.end()) { cout << "find fail\n"; } else { cout << "find success\n"; } // find success } int main() { play01(); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
STL算法设计理念 - 二元函数,二元谓词以及在set中的应用
原文地址:http://blog.csdn.net/zyq522376829/article/details/46820927