1.自定义仿函数
STL提供了算术类、相对关系类、逻辑运算类的预定义仿函数,但为了满足程序的需要,大多数情况下需要我们自己定义仿函数。如果希望它们能够和函数适配器搭配使用,就必须满足某种条件:必须提供一些类型成员来反映其参数和返回值的类别。
STL提供了以下两种结构:
template<class Arg, class Result>
struct unary_function {
typedef Arg argument_type;
typedef Result result_type;
};
template<class Arg1, class Arg2, class Result>
struct binary_function {
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;
};
自定义仿函数只需继承上述形式之一,就能满足“可适配”的条件。
例如定义一个计算参数幂的仿函数及使用:
#include <iostream>
#include <cmath>
#include <vector>
#include <functional>
#include <algorithm>
#include <iterator>
using namespace std;
template<class T1, class T2>
struct fpow : public binary_function<T1, T2, T1>{
T1 operator() (const T1& base, const T2& exp) const{
return pow(base, exp);
}
};
int main(){
vector<int> ivec;
for(int i = 1; i <= 9; ++i)
ivec.push_back(i);
copy(ivec.begin(), ivec.end(),
ostream_iterator<int>(cout, " "));
cout << endl;
transform(ivec.begin(), ivec.end(),
ostream_iterator<int>(cout, " "),
bind2nd(fpow<float, int>(), 3));
cout << endl;
transform(ivec.begin(), ivec.end(),
ostream_iterator<int>(cout, " "),
bind1st(fpow<float, int>(), 3));
return 0;
}
2.自定义组合型适配器
可以用简单的仿函数构造出复杂的仿函数,一般而言,所有函数行为都可以通过仿函数的组合来实现,但STL并没有提供足够的函数适配器来完成这项工作,例如,我们无法将两个一元运算A和B组合成运算“A and B”.
以下四种常用的组合型适配器:
①f(g(elem)):g()的结果作为f()的参数,整个表达式接收一个参数elem,所以类似一个一元判断式;
示例:作(x + 10) * 5运算:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <functional>
#include <vector>
using namespace std;
template<class OP1, class OP2>
class compose_f_gx_t: public unary_function<typename OP2::argument_type,
typename OP1::result_type>{
private:
OP1 op1;
OP2 op2;
public:
compose_f_gx_t(const OP1& o1, const OP2& o2): op1(o1), op2(o2) {}
typename OP1::result_type operator() (const typename OP2::argument_type& x){
return op1(op2(x));
}
};
template<class OP1, class OP2> //函数模板可以自动匹配模板形参,调用时较方便
inline compose_f_gx_t<OP1, OP2> compose_f_gx(const OP1& op1, const OP2& op2){
return compose_f_gx_t<OP1, OP2>(op1, op2);
}
int main(){
vector<int> ivec;
for(int i = 1; i <= 9; ++i)
ivec.push_back(i);
transform(ivec.begin(), ivec.end(),
ostream_iterator<int>(cout, " "),
compose_f_gx(bind2nd(multiplies<int>(), 5),
bind2nd(plus<int>(), 10)));
return 0;
}
②f(g(elem1,elem2)):两元素elem1和elem2作为参数传递给g(),其结果再作为参数传给一元判断式f(),整个表达式的操作类似一个二元判断式;
③f(g(elem),h(elem)):elem作为参数传递给两个一元判断式g()和h(),两者的结果再交给f处理,由于整个表达式只接收一个参数elem,故表达式的操作类似于一个一元判断式;
示例:4 < x < 7
#include <iostream>
#include <iterator>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
template<class OP1, class OP2, class OP3>
class compose_f_gx_hx_t:public unary_function<typename OP2::argument_type,
typename OP1::result_type>{
private:
OP1 op1;
OP2 op2;
OP3 op3;
public:
compose_f_gx_hx_t(const OP1& o1, const OP2& o2, const OP3& o3): op1(o1), op2(o2), op3(o3) {}
typename OP1::result_type operator() (const typename OP2::argument_type& x){
return op1(op2(x), op3(x));
}
};
template<class OP1, class OP2, class OP3>
inline compose_f_gx_hx_t<OP1, OP2, OP3> compose_f_gx_hx(const OP1& op1, const OP2& op2, const OP3& op3){
return compose_f_gx_hx_t<OP1, OP2, OP3>(op1, op2, op3);
}
int main(){
vector<int> ivec;
for(int i = 1; i <= 9; ++i)
ivec.push_back(i);
copy(ivec.begin(), ivec.end(), ostream_iterator<int>(cout, " "));
cout << endl;
vector<int>::iterator pos;
pos = remove_if(ivec.begin(), ivec.end(),
compose_f_gx_hx(logical_and<bool>(),
bind2nd(greater<int>(), 4),
bind2nd(less<int>(), 7)));
ivec.erase(pos, ivec.end());
copy(ivec.begin(), ivec.end(), ostream_iterator<int>(cout, " "));
return 0;
}
④f(g(elem1),h(elem2)):elem1和elem2作为参数分别传递给一元判断式g()和h(),两个结果作为参数传递给f处理。整个表达式接收两个参数elem1和elem2,故表达式的操作类似于一个一元判断式
示例:不区分大小写字符串匹配
#include <iostream>
#include <algorithm>
#include <functional>
#include <cctype>
#include <vector>
#include <iterator>
#include <string>
using namespace std;
template<class OP1, class OP2, class OP3>
class compose_f_gx_hy_t
: public binary_function<typename OP2::argument_type,
typename OP3::argument_type,
typename OP1::result_type>
{
private:
OP1 op1;
OP2 op2;
OP3 op3;
public:
compose_f_gx_hy_t(const OP1& o1, const OP2& o2, const OP3& o3)
: op1(o1), op2(o2), op3(o3) {}
typename OP1::result_type operator() (const typename OP2::argument_type& x,
const typename OP3::argument_type& y){
return op1(op2(x), op3(y));
}
};
template<class OP1, class OP2, class OP3>
inline compose_f_gx_hy_t<OP1, OP2, OP3>
compose_f_gx_hy(const OP1& o1, const OP2& o2, const OP3& o3){
return compose_f_gx_hy_t<OP1, OP2, OP3>(o1, o2, o3);
}
int main(){
string s = "Internationalization";
string sub = "NATION";
string::iterator pos = search(s.begin(), s.end(),
sub.begin(), sub.end(),
compose_f_gx_hy(equal_to<int>(),
ptr_fun(::toupper),
ptr_fun(::toupper)));
if(pos != s.end())
cout << "\"" << sub << " is part of " << s << endl;
return 0;
}
注:不能将f的行为等同于表达式的行为,因为在组合型适配器中,f只是这个表示中的一个模板参数而已,f有自己的argment_type、result_type,表达式将这些函数进行组合得到结果。
可见组合型函数适配器的定义也很简单,首先判断表达式是几元判断式,确定参数/返回值类别后进行继承,定义operator操作符,return表达式即可。