c++之STL(9)重载函数调用操作符 函数对象 谓词
2016-07-28 08:45
561 查看
重载函数调用操作符
函数对象:定义了调用操作符的类,其对象称为“函数对象”
如果函数对象的返回值是bool 类型的,那么我们就称为它为谓词
一元函数对象 :函数返回一个值
一元谓词 :若返回值有一个对象,并且为bool类型的,
二元函数:对象函数对象有两个参数
二元谓词:函数返回为bool
二元函数对象和二元谓词
函数对象:定义了调用操作符的类,其对象称为“函数对象”
如果函数对象的返回值是bool 类型的,那么我们就称为它为谓词
一元函数对象 :函数返回一个值
一元谓词 :若返回值有一个对象,并且为bool类型的,
二元函数:对象函数对象有两个参数
二元谓词:函数返回为bool
#include<iostream> // #include<vector> #include<list> #include<algorithm> using namespace std; //class absInt{ // //public: // int operator()(int val){ // // } //}; // struct 里默认是公用的 struct absInt{ // 重载操作符:函数调用操作符 int operator()(int val){ return val < 0 ? -val : val; } }; // 普通函数 template<typename elementType> void FuncDisplayElement(const elementType &element) { cout << element << endl; }; // 函数对象 类是有数据成员的,那么就可以保持状态 template<typename elementType> struct DisplayElement { void operator()(const elementType &element) const { cout << element << endl; } }; int main() { int i = -42; absInt absObj; unsigned int ui = absObj(i); cout << ui << endl; cout << "Hello 函数对象!" << endl; vector<int> a; for (int i = 0; i < 10; i++) { a.push_back(i); } list<char> b; for (char c = 'a'; c < 'k'; ++c) { b.push_back(c); } // STL算法 for_each(a.begin(), a.end(), DisplayElement<int>()); cout << endl; for_each(b.begin(), b.end(), DisplayElement<char>()); cout << endl; // system("pause"); return 0; }
#include<iostream> #include<vector> #include<algorithm> using namespace std; // 函数对象 template<typename numberType> struct IsMutiple// 默认是公认的 { numberType m_Divisor; IsMutiple(const numberType &divisor) { m_Divisor = divisor; } bool operator()(const numberType &element)const { return((element % m_Divisor) == 0); } }; int main() { IsMutiple<int> a(4); vector<int> vecIntergers; for (int i = 25; i < 100; i++) { vecIntergers.push_back(i); } vector<int>::iterator iElement; iElement = find_if(vecIntergers.begin(), vecIntergers.end(), a); if (iElement != vecIntergers.end()) { cout << "第一个4的整数倍的数是:" << *iElement<< endl; } // system("pause"); return 0; }
#include<iostream> #include<vector> #include<algorithm> using namespace std; // 函数对象 一元谓词 template<typename numberType> struct IsMutiple// 默认是公认的 { numberType m_Divisor; IsMutiple(const numberType &divisor) { m_Divisor = divisor; } bool operator()(const numberType &element)const { return((element % m_Divisor) == 0); } }; int main() { //IsMutiple<int> a(4); vector<int> vecIntergers; for (int i = 25; i < 100; i++) { vecIntergers.push_back(i); } vector<int>::iterator iElement; iElement = find_if(vecIntergers.begin(), vecIntergers.end(), IsMutiple<int>(4)); if (iElement != vecIntergers.end()) { cout << "第一个4的整数倍的数是:" << *iElement<< endl; } // system("pause"); return 0; }
二元函数对象和二元谓词
#include<iostream> #include<vector> #include<algorithm> using namespace std; //二元函数对象 这个不是谓词,只有返回值是bool,才被称为谓词 template<typename elementType> class CMultiply { public: elementType operator()(const elementType &elem1, const elementType &elem2) { return elem1 * elem2; } }; int main() { vector<int> a, b; for (int i = 0; i < 10; i++) a.push_back(i); for (int j = 100; j < 110; j++) b.push_back(j); vector<int> vecResult; vecResult.resize(10); // 设置大小10个 // STL 算法 transform(a.begin(), a.end(), b.begin(), vecResult.begin(), CMultiply<int>()); // 对向量进行循环 for (size_t nIndex = 0; nIndex < vecResult.size(); nIndex++) { cout << vecResult[nIndex] << endl; } // system("pause"); return 0; }
#include<iostream> #include<set> #include<string> #include<algorithm> using namespace std; // 二元谓词 改变容器默认的行为 class CCompareStringNoCase { public: bool operator()(const string & str1, const string &str2)const { string str1LowerCase; str1LowerCase.resize(str1.size()); transform(str1.begin(), str1.end(), str1LowerCase.begin(), tolower); // string str2LowerCase; str2LowerCase.resize(str2.size()); transform(str2.begin(), str2.end(), str2LowerCase.begin(), tolower); return(str1LowerCase < str2LowerCase); } }; int main() { set<string> xm; set<string, CCompareStringNoCase> names;// 不分大小写,自己定义的一个set 容器 names.insert("Tina"); names.insert("jim"); names.insert("Jack"); names.insert("Sam"); names.insert("hello"); set<string>::iterator iNameFound = names.find("jim"); if (iNameFound != names.end()) { cout << "找到了:" << *iNameFound << endl; } else { cout << "" << endl; } // system("pause"); return 0; }
相关文章推荐
- C语言中的关键字
- UVA1615 高速公路(highway)(重庆一中高2018级信息学竞赛测验5) 解题报告
- 程序设计入门——C语言笔记(第0周-第3周)
- c++之STL(8)优先级队列 priority_queue
- c/c++ cin>> cout<< scanf() printf() 返回值
- 实习点滴 - 破窗理论、C++ 函数模板静态库
- 优先队列--C语言实现与Java例子
- C++中标准类string常用示例
- c++对象内存模型分析工具
- C++中string和int之间的转换
- leetcode 328. Odd Even Linked List 16ms beats 96.25% c++
- hdoj1879 继续畅通工程
- hdoj1233 还是畅通工程
- hdoj1232 畅通工程
- hdoj 1863 畅通工程
- hdoj1875 畅通工程再续
- hdoj5734 Acperience
- hdoj5742 It's All In The Mind
- hdoj4497
- hdoj5428 The Factor