STL预定义函数对象
2018-01-15 16:26
330 查看
预定义函数对象基本概念:标准模板库STL提前定义了很多预定义函数对象
1)使用预定义函数对象:
[cpp] view
plain copy
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <functional>
using namespace std;
// plus,预定义好的函数对象,能实现不同类型数据的 + 运算
// 实现了数据类型和算法的分离,通过函数对象技术实现的
void play01()
{
/* plus函数对象原型
template<class _Ty = void>
struct plus
: public binary_function < _Ty, _Ty, _Ty >
{ // functor for operator+
_Ty operator()(const _Ty& _Left, const _Ty& _Right) const
{ // apply operator+ to operands
return (_Left + _Right);
}
};
*/
plus<int> intAdd; // 预定义函数对象
int x = 10;
int y = 20;
int z = intAdd(x, y); // x + y;
cout << "z: " << z << endl;
// z : 30
plus<string> stringAdd;
string s1 = "lucifer";
string s2 = "zhang";
string s3 = stringAdd(s1, s2);
cout << "s3: " << s3 << endl;
// s3: luciferzhang
}
void play02()
{
vector<string> v;
v.push_back("lucifer");
v.push_back("zhang");
v.push_back("yao");
v.push_back("qi");
/*
template<class _Ty = void>
struct greater
: public binary_function < _Ty, _Ty, bool >
{ // functor for operator>
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{ // apply operator> to operands
return (_Left > _Right);
}
};
*/
//缺省情况下,sort()用底层元素类型的小于操作符以升序排列容器的元素。
//为了降序,可以传递预定义的类模板greater,它调用底层元素类型的大于操作符:
sort(v.begin(), v.end(), greater<string>()); // 从大到小排序
for (vector<string>::iterator it = v.begin(); it != v.end(); ++it) {
cout << *it << ' ';
}
cout << endl;
// zhang yao qi lucifer
string sl = "lucifer";
int num = count_if(v.begin(), v.end(), bind2nd(equal_to<string>(), sl));
cout << "count of 'lucifer': " << num << endl;
// count of 'lucifer': 1
}
int main()
{
play01();
play02();
return 0;
}
2)算术函数对象 预定义的函数对象支持加、减、乘、除、求余和取反。调用的操作符是与type相关联的实例
加法:plus<Types>
plus<string> stringAdd;
sres = stringAdd(sva1,sva2);
减法:minus<Types>
乘法:multiplies<Types>
除法divides<Tpye>
求余:modulus<Tpye>
取反:negate<Type>
negate<int> intNegate;
ires = intNegate(ires);
Ires= UnaryFunc(negate<int>(),Ival1);
3)关系函数对象
等于equal_to<Tpye>
equal_to<string> stringEqual;
sres = stringEqual(sval1,sval2);
不等于not_equal_to<Type>
大于 greater<Type>
大于等于greater_equal<Type>
小于 less<Type>
小于等于less_equal<Type>
4)逻辑函数对象
逻辑与 logical_and<Type>
logical_and<int> indAnd;
ires = intAnd(ival1,ival2);
dres=BinaryFunc( logical_and<double>(),dval1,dval2);
逻辑或logical_or<Type>
逻辑非logical_not<Type>
logical_not<int> IntNot;
Ires = IntNot(ival1);
Dres=UnaryFunc( logical_not<double>,dval1);
1)使用预定义函数对象:
[cpp] view
plain copy
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <functional>
using namespace std;
// plus,预定义好的函数对象,能实现不同类型数据的 + 运算
// 实现了数据类型和算法的分离,通过函数对象技术实现的
void play01()
{
/* plus函数对象原型
template<class _Ty = void>
struct plus
: public binary_function < _Ty, _Ty, _Ty >
{ // functor for operator+
_Ty operator()(const _Ty& _Left, const _Ty& _Right) const
{ // apply operator+ to operands
return (_Left + _Right);
}
};
*/
plus<int> intAdd; // 预定义函数对象
int x = 10;
int y = 20;
int z = intAdd(x, y); // x + y;
cout << "z: " << z << endl;
// z : 30
plus<string> stringAdd;
string s1 = "lucifer";
string s2 = "zhang";
string s3 = stringAdd(s1, s2);
cout << "s3: " << s3 << endl;
// s3: luciferzhang
}
void play02()
{
vector<string> v;
v.push_back("lucifer");
v.push_back("zhang");
v.push_back("yao");
v.push_back("qi");
/*
template<class _Ty = void>
struct greater
: public binary_function < _Ty, _Ty, bool >
{ // functor for operator>
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{ // apply operator> to operands
return (_Left > _Right);
}
};
*/
//缺省情况下,sort()用底层元素类型的小于操作符以升序排列容器的元素。
//为了降序,可以传递预定义的类模板greater,它调用底层元素类型的大于操作符:
sort(v.begin(), v.end(), greater<string>()); // 从大到小排序
for (vector<string>::iterator it = v.begin(); it != v.end(); ++it) {
cout << *it << ' ';
}
cout << endl;
// zhang yao qi lucifer
string sl = "lucifer";
int num = count_if(v.begin(), v.end(), bind2nd(equal_to<string>(), sl));
cout << "count of 'lucifer': " << num << endl;
// count of 'lucifer': 1
}
int main()
{
play01();
play02();
return 0;
}
2)算术函数对象 预定义的函数对象支持加、减、乘、除、求余和取反。调用的操作符是与type相关联的实例
加法:plus<Types>
plus<string> stringAdd;
sres = stringAdd(sva1,sva2);
减法:minus<Types>
乘法:multiplies<Types>
除法divides<Tpye>
求余:modulus<Tpye>
取反:negate<Type>
negate<int> intNegate;
ires = intNegate(ires);
Ires= UnaryFunc(negate<int>(),Ival1);
3)关系函数对象
等于equal_to<Tpye>
equal_to<string> stringEqual;
sres = stringEqual(sval1,sval2);
不等于not_equal_to<Type>
大于 greater<Type>
大于等于greater_equal<Type>
小于 less<Type>
小于等于less_equal<Type>
4)逻辑函数对象
逻辑与 logical_and<Type>
logical_and<int> indAnd;
ires = intAnd(ival1,ival2);
dres=BinaryFunc( logical_and<double>(),dval1,dval2);
逻辑或logical_or<Type>
逻辑非logical_not<Type>
logical_not<int> IntNot;
Ires = IntNot(ival1);
Dres=UnaryFunc( logical_not<double>,dval1);
相关文章推荐
- STL函数对象:定义、及其在STL中的应用
- stl算法设计理念_预定义函数对象和函数适配器1
- stl算法设计理念_预定义函数对象和函数适配器2_案例
- STL函数对象之预定义函数对象和函数对象绑定器
- STL算法设计理念 - 预定义函数对象
- STL算法设计理念 - 预定义函数对象
- 工厂模式:封装对象的创建(一、在基类中定义一个静态成员函数)
- STL中的函数对象(Funciont Objects)
- C++ Primer Plus学习笔记之STL函数对象
- 预定义函数对象
- 对象作为函数参数、类外定义成员函数
- js函数定义方式(包括嵌套,对象等)
- 第十五周项目 程序填空(1) 运用STL函数对象
- 从零开始学C++之STL(八):函数对象、 函数对象与容器、函数对象与算法
- Oracle 查看 表 存储过程 触发器 函数 等对象定义语句的方法
- JS 之函数定义 & 创建对象(三种方式)
- 【读书笔记】【STL】定义序列式容器对象的五种方式
- 标准库定义的函数对象
- python基础----函数的定义和调用、return语句、变量作用域、传参、函数嵌套、函数对象、闭包、递归函数
- C++ STL(29):Function Object Adapter(函数对象适配器)