Demo two-tuple function object
#include <iostream> #include <cstdio> #include <vector> #include <algorithm>using namespace std ; template <typename t>class sumvector{public:t operator () (t T1, T T2)//two-tuple function object {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 (transform); V1.begin (V1.end (), V2.begin (), (), V3.begin (), sumvector<int> ());/* Transform function prototype 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 returns the starting position of the result iterator for (vector<int>::iterator it = V3.begin(); It! = V3.end (); ++it) {cout << *it << ';} cout << endl;//3 7 0 0 0 0 0 0 0}int main () {play01 (); return 0;}
Demo $ two predicate
#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 << ';} BINARY predicate bool Mycompare (const int &A, const int &b) {return a < b;} void Play01 () {vector<int> V (Ten) Srand (time (0)), for (int i = 0; i <; i++) {int tmp = rand ()% 100;v[i] = tmp;} Printvector (v);//94-74for_each (V.begin (), V.end (), FuncShowElemt2), cout << endl;//90 19 94 74sort (V.begin (), V.end (), Mycompare) For_each (V.begin (), V.end (), FuncShowElemt2); cout << endl;/ /0 8 80}int Main () {play01 (); return 0;}
Application of the demo two-yuan predicate in set
#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"); The Find function defaults to case if (it1 = = Set1.end ()) {cout << "find fail\n";} else {cout << "find success\n";} Find Failset<string, comparenocase> set2;set2.insert ("Lucifer"); Set2.insert ("Zhang"); Set2.insert ("Yaoqi") ; set<string, Comparenocase>::iterator it2 = Set2.find ("LUcifer"); The Find function defaults to case if (it2 = = Set2.end ()) {cout << "find fail\n";} else {cout << "Find success\n";} Find Success}int Main () {play01 (); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
STL algorithm design Concept-two-element function, two-yuan predicate and its application in set