function object即functor(仿函数):定义了operator()的对象
function adapter(函数适配器)
binder(绑定器)
functional composition(机能合成)
binder(绑定器)
bind()用来将参数绑定于可调用对象(call object)
包括2类:
(1).被传入的实参,利用预定义占位符_1、_2、…,定义于std::placeholders
(2).明白指出的实参,写出其名称
调用一般函数
using namespace std::placeholders;
//间接调用:计算某传入的参数和数字10之和
auto test = std::bind(std::plus<int>(),
std::placeholders::_1,
10);
test(55);//调用bind的函数,计算55+10
//Lambda版本
auto test = [](int i){ return i+10; };
test(55);;
//直接调用binder,计算32+10
std::bind(std::plus<int>(), _1, 10)(32);
调用成员函数
class Person{
...
void print() const {...}
void print2(const string& str) const{...}
...
}
std::vector<Person> coll = {...};
//coll内每个Person调用各自的print()
for_each(coll.begin(), coll.end(),
std::bind(&Person::print, _1));
//调用各自的print2("Person: ")
for_each(coll.begin(), coll.end(),
std::bind(&Person::print2, _1, "Person: "));
Person n("nico");
bind(&Person::print2, _1, "it's")(n);//等同n.print2("it's");
Function Object(函数对象)
function object即functor(仿函数):定义了operator()的对象
class FunctionObjectType {
public:
void operator() () {
statements
}
}
FunctionObjectType fo;
...
fo(...);//调用函数对象的operator(),而非调用函数fo()
- 函数对象3大有点
- 比一般函数更smart,因为它可以拥有状态。
- 每个function object都有其类型
- function object通常比function pointer更快
以Function Object为排序准则
//fo/sort1.cpp
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
class Person {
public:
std::string firstname() const;
std::string lastname() const;
...
};
//- operator() returns whether a person is less than another person
class PersonSortCriterion {
public:
bool operator() (cosnt Person& p1, const Person& p2) const{
return p1.lastname() < p2.lastname() ||
(p1.lastname() == p2.lastname() &&
p1.firstname() < p2.firstname());
}
};
int main() {
//create a set with special soring criterion
//排序准则PersonSortCriterion是一个class,其他函数则不会这么简洁
std::set<Person, PersonSortCriterion> coll;
...
for (auto pos = coll.begin(); pos != coll.end(); ++pos) {...}
...
}
Function Object拥有内部状态(Internal State)
function object行为像个函数的同时又拥有多个状态
// fo/sequence1.cpp
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
// #include
class IntSequence {
public:
IntSequence(int val)
: value(val){
}
int operator() (){
return ++value;
}
private:
int value;
};
int main(){
std::list<int> coll;
//pass by value
//insert values from 1 to 9
generate_n(back_insert(coll), //start
9, //number of elements
IntSequence(1)); //函数对象
//pass by reference
IntSequence seq(1);
generate_n<back_insert_iterator<std::list<int>>,
int,
IntSequence&>(
back_insert(coll), //start
4,
seq
);
}
for_each的返回值
for_each()可以传回其function object,通过for_each()的返回值获取function_object的状态
// fo/foreach3.cpp
#include <iostream>
#include <vector>
#include <algorithm>
//function object to process the mean value
class MeanValue {
public:
MeanValue():num(0), sum(0){}
//- process one more element of the sequence
void operator() (int elem) {
++num;
sum += elem;
}
//- return mean value
double value() {
return static_cast<double>(sum)/static_cast<double>(num);
}
private:
long num; //number of elements
long sum; //sum of all element values
};
int main(){
std::vector<int> coll = {1,2,3,4,5,6,7,8};
//- process and print mean value
//for_each返回的函数对象被赋值给mv
MeanValue mv = for_each(coll.begin(), coll.end(),//range
MeanValue());//operation,返回的
std::cout<<"mean value:"<<mv.value()<<std::endl;
}