45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:STL中的函数对象的详细介绍

STL中的函数对象的详细介绍

2016-09-09 03:37:19 来源:www.45fan.com 【

STL中的函数对象的详细介绍

概要
一个函数对象(Function Object或者Functor)简单的说就是能够以函数调用的形式出现的任何东西。一个普通的函数显然就是一个函数对象,函数指针也是,更一般的,一个定义了operator()的Class也是。

描述
基本的函数对象的概念有Generator,Unary Function(一元函数),Binary Function(二元函数):他们各自表示能以F(),F(x),F(x,y)的形式出现的函数对象。当让这些可以扩展为Ternary Funtion甚至更多,但是实际上没有哪个STL算法用到了两个以上参数的函数对象。其他所有STL定义的函数对象的感念都是这三个基本概念的细化(refinements)。

返回bool类型的函数对象是相当重要的一类函数对象。一个返回bool值的Unary Function称作Predicate(谓词),相应的返回bool值的Binary Function称作Binary Predicate(二元谓词)。

在Function Objects和Adaptable Function Objects(可适应的函数对象)之间有重要但是有些微妙的区别。一般地说,虽然一个Function Objects对它的参数的类型有要求,但是操作符operator()可以重载,可以是模板,或者两者兼有。也就是说,没有准确地方法获得这个Function Objects的参数和返回类型的信息。但是一个Adaptable Function Objects必须以typedef的形式指定他的参数和返回值得类型。比如,类型F0是Adaptable Function Objects的模型,那么必须定义F0::result_type。类似的,如果F1是Adaptable Unary Function Objects的模型,那么F1::argument_type和F1::result_type必须有定义;如果F2是Adaptable Binary Function Objects的模型,那么必须定义F2::first_argument_type,F2::second_argument_type和F2::result_type。STL提供了基类 unary_function和binary_function来简化Adaptable Unary Functions和Adaptable Binary Functions的模型的定义。

Adaptable Function Objects是非常重要的,因为他们可以被function object adaptors(函数对象适配器,用来操作和控制其它函数对象)使用。STL提供了许多function object adaptors,包括unary_negate,unary_compose和binary_compose,用来对函数对象进行组合。

最后,STL包括了许多不同的预定义的函数对象,包括算子(plus,minus,multiplies,divides,modulus和negate),算术比较(equal_to,not_equal_to,greater,less,greater_equal和less_equal),和逻辑操作(logical_and,logical_or和logical_not)。这样你就可以不用手动写新的函数对象而是用这些函数对象就可以组合出相当复杂的操作。

例子
将一个vector<int>用随机数填充。这里,一个函数指针就是一个函数对象。
vector<int> V(100);
generate(V.begin(), V.end(), rand);

按照绝对值大小排序一个vector<int>。这里,函数对象是一个用户定义的class类型。

struct less_mag : public binary_function<double, double, bool> {
bool operator()(double x, double y) { return fabs(x) < fabs(y); }
};

vector<double> V;
...
sort(V.begin(), V.end(), less_mag());

对一个vector<int>进行求和。这里,函数对象是一个可以保存状态的用户定义类型。

struct adder : public unary_function<double, void>
{
adder() : sum(0) {}
double sum;
void operator()(double x) { sum += x; }
};

vector<double> V;
...
adder result = for_each(V.begin(), V.end(), adder()); [3]
cout << "The sum is " << result.sum << endl;

删除list<int>中所有大于100且小于1000的元素

list<int> L;
...
list<int>::iterator new_end =
remove_if(L.begin(), L.end(),
compose2(logical_and<bool>(),
bind2nd(greater<int>(), 100),
bind2nd(less<int>(), 1000)));
L.erase(new_end, L.end());

附表
概念 Concepts

Generator
Unary Function
Binary Function

Predicate
Binary Predicate

Adaptable Generator
Adaptable Unary Function
Adaptable Binary Function
Adaptable Predicate
Adaptable Binary Predicate

类型 Types
plus
minus
multiplies (formerly called times)
divides
modulus,
negate
equal_to
not_equal_to
greater
less
greater_equal
less_equal,
logical_and
logical_or
logical_not
subtractive_rng
identity
project1st
project2nd
select1st
select2nd
unary_function
binary_function
unary_compose
binary_compose
unary_negate
binary_negate
binder1st
binder2nd
pointer_to_unary_function
pointer_to_binary_function

函数 Functions
compose1
compose2
not1
not2
bind1st
bind2nd
ptr_fun

 

本文地址:http://www.45fan.com/a/question/73795.html
Tags: 中的 函数 STL
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部