STL算法设计理念 - 二元函数,二元谓词以及在set中的应用
2015-07-09 19:41
423 查看
demo 二元函数对象
demo 二元谓词
demo 二元谓词在set中的应用
#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; }
demo 二元谓词
#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; }
demo 二元谓词在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"); // 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中的应用
- Algorithms—58.Length of Last Word
- C++ HOJ 二阶魔方
- 你不知道的用例编写方法
- 长期接收碎片化的知识有什么弊端?
- C# 工具库
- JAVA注解
- 关于 App.config文件出错,配置系统未能初始化。 问题解决方案
- sql查询优化 索引优化
- Codeforces 534A Exam
- NYOJ 216 A problem is easy
- 打印数组中的汉字
- ZOJ 1047 Image Perimeters
- ehcache
- Python: The _imagingft C module is not installed错误的解决
- 从字符数组中删除特定字符
- ICONFONT的使用方法
- 搭建安卓开发环境
- 小胖学PHP总结4-----PHP的字符串操作
- 15个信号说明你是过度思考者