STL之函数对象
2014-04-14 17:20
363 查看
目录
为了使类属算法具有灵活性,STL常使用函数的重载机制为算法提供两种形式。算法的第一种形式使用的是常规的操作来实现。第二种形式中,算法可以根据用户指定的准测对元素经行处理。
函数对象包含了一个可以通过函数调用运算符()使用的函数。实际上,函数对象是重载了函数调用运算符operator()的类模板。
用户可以创建自己的函数对象。STL提供了算术函数对象,关系函数对象,逻辑函数对象。
算术函数对象:
plus<type> 加
minus<type> 减
multiplies<type> 乘
divides<type> 除
modulus<type> 模
negate<type> 取反
示例代码:
运行结果:
intQueue.front:26
intQueue.back:33
intQueue.front:18
intQueue :
18 50 33
关系函数对象:
equal_to<type> 等于
not_equal_to<type> 不等于
greater<type> 大于
greater_equal<type> 大于等于
less<type> 小于
less_equal<type> 小于等于
示例代码:
运行结果:
isEqual=1
there is greater than hello
vecList:
2 3 4 5 1 7 8 9
intItr1:5,intItr2:1
psition:4
psition:4
逻辑运算对象:
logical_not<type>
logical_and<type>
logical_or<type>
为了使类属算法具有灵活性,STL常使用函数的重载机制为算法提供两种形式。算法的第一种形式使用的是常规的操作来实现。第二种形式中,算法可以根据用户指定的准测对元素经行处理。
函数对象包含了一个可以通过函数调用运算符()使用的函数。实际上,函数对象是重载了函数调用运算符operator()的类模板。
用户可以创建自己的函数对象。STL提供了算术函数对象,关系函数对象,逻辑函数对象。
算术函数对象:
plus<type> 加
minus<type> 减
multiplies<type> 乘
divides<type> 除
modulus<type> 模
negate<type> 取反
示例代码:
#include <iostream> #include <algorithm> #include <string> #include <numeric> #include <iterator> #include <vector> #include <functional> using namespace std; int functAdd(plus<int>,int,int); int main() { // 加法函数对象 plus<int> addNum; int sum = addNum(34,56); cout << "Sum=" << sum << endl; // 字符串拼接 plus<string> joinString; string s1 = "hello"; string s2 = "There"; string str = joinString(s1,s2); cout << "str=" << str << endl; // 调用外部函数 cout << "funcAdd=" << functAdd(addNum,34,26) << endl; int list[8] = {1,2,3,4,5,6,7,8}; vector<int> intsList(list,list+8); ostream_iterator<int> screenOut(cout, " "); cout << "intList:"; copy(intsList.begin(),intsList.end(),screenOut); cout << endl; //累计 int suma = accumulate(intsList.begin(),intsList.end(),0); cout << "accumulate:" << suma << endl; int product = accumulate(intsList.begin(),intsList.end(),1,multiplies<int>()); cout << "product:" << product << endl; return 0; } int functAdd(plus<int> sum,int x,int y) { return sum(x, y); }
运行结果:
intQueue.front:26
intQueue.back:33
intQueue.front:18
intQueue :
18 50 33
关系函数对象:
equal_to<type> 等于
not_equal_to<type> 不等于
greater<type> 大于
greater_equal<type> 大于等于
less<type> 小于
less_equal<type> 小于等于
示例代码:
#include <iostream> #include <algorithm> #include <string> #include <numeric> #include <iterator> #include <vector> #include <functional> using namespace std; int main() { // 等于 equal_to<int> compare; bool isEqual = compare(6,6); cout << "isEqual=" << isEqual << endl; // 大于 greater<string> greaterString; string s1 = "hello"; string s2 = "there"; if (greaterString(s1, s2)) { cout << s1 << " is greater than " << s2 << endl; } else { cout << s2 << " is greater than " << s1 << endl; } int temp[8] = {2,3,4,5,1,7,8,9}; vector<int> vecList(temp,temp+8); vector<int>::iterator intItr1,intItr2; ostream_iterator<int> screen(cout, " "); cout << "vecList:" <<endl; copy(vecList.begin(),vecList.end(),screen); cout << endl; intItr1 = adjacent_find(vecList.begin(),vecList.end(),greater<int>()); intItr2 = intItr1 + 1; cout << "intItr1:" << *intItr1 <<",intItr2:" << *intItr2 << endl; cout << "psition:" << vecList.end() - intItr2 << endl; cout << "psition:" << intItr2 - vecList.begin() << endl; return 0; }
运行结果:
isEqual=1
there is greater than hello
vecList:
2 3 4 5 1 7 8 9
intItr1:5,intItr2:1
psition:4
psition:4
逻辑运算对象:
logical_not<type>
logical_and<type>
logical_or<type>
相关文章推荐
- STL预定义函数对象
- # 十、STL函数对象---函数对象的概念
- STL中的函数对象(Funciont Objects)
- C++ STL 函数对象简介
- STL知识点(常用算法函数介绍 、容器、类属算法、迭代器、函数对象、适配器、分配器
- C++STL之函数对象及谓词
- STL学习笔记8 -- 函数对象
- stl基本容器学习——函数对象
- 模板函数与模板类的区别。模板函数允许隐式调用,所以STL算法允许传入函数指针,也允许传入函数对象
- STL 算法中函数对象和谓词
- STL算法之回调函数和函数对象的理解及设计
- C++STL之函数对象及谓词
- STL之函数对象
- STL中的函数对象
- C++STL之函数对象及谓词
- STL中伪函数、函数对象(functor)初步理解(下)
- 【学习笔记】C++高级程序设计:STL:函数对象
- STL函数对象当参数和返回值
- STL中的函数对象
- 泛型编程与STL(二):函数对象