一:适配器简介
C++中有三类适配器,分别是
容器适配器,
迭代器适配器和
函数适配器,这里
主要介绍函数适配器。
(一)函数适配器简介
STL中已经定义了大量的函数对象,但是有时候需要对函数返回值进行进一步的简单计算,或者填上多余的参数,才可以带入其他的算法中进行下一步数据处理,不能直接带入算法。
函数适配器就实现了这一功能:
将一种函数对象转化为另外一种符合要求的函数对象。
函数适配器可以分为4大类:绑定适配器(bind adaptor),组合适配器(composite adaptor),指针函数适配器(pointer adaptor),成员函数适配器(member function adaptor)
(二)绑定适配器(bind adaptor)
(三)组合适配器(composite adaptor)
(四) 指针函数适配器(pointer adaptor)
(五)成员函数适配器(member function adaptor)
二:函数适配器辅助函数
直接构造STL中的函数适配器通常会导致冗长的类型声明。---->之前说过模板函数和STL中类型是严格定义的,需要我们显式写出
为了简化函数适配器的构造,STL还提供了函数适配器辅助函数,借助于泛型自动推断技术,无需显式的类型声明便可以实现函数适配器的构造。
三:常用函数适配器
标准库提供一组函数适配器,用来特殊化或者扩展一元和二元函数对象。
(一)绑定器(binder): 将二元函数对象转一元函数对象
binder通过把二元函数对象的一个实参绑定到一个特殊的值上,将其转换成一元函数对象。C++标准库提供
两种预定义的binder适配器(适配器辅助函数):
bind1st和bind2nd,前者把值绑定到二元函数对象的第一个实参上,后者绑定在第二个实参上。
(二)取反器(negator) : 操作谓词函数
negator是一个将函数对象的值翻转的函数适配器。标准库提供
两个预定义的ngeator适配器(适配器辅助函数):
not1翻转一元预定义函数对象的真值,而not2翻转二元谓词函数的真值。
(三)常用函数适配器案例:《重点》
1.使用绑定器和预定义函数对象
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
//由于要使用到预定义函数对象,所以引入
using namespace std;
template<typename T>
void ShowEle(
const T& t)
//用于打印容器数据
{
cout << t <<
" ";
}
int main()
{
vector<
int>
v1, v2, v3;
for (
int i =
0; i <
10;i++
)
v1.push_back(rand() %
30);
//v1数据插入
//打印数据
for_each(v1.begin(), v1.end(), ShowEle<
int>
);
cout <<
endl;
int num = count_if(v1.begin(), v1.end(), bind2nd(greater<int>(), 2));
cout << num <<
endl;
system("pause");
return 0;
}
补充:count和count_if
1)count(first,last,value):first是容器的首迭代器,last是容器的末迭代器,value是询问的元素,整个函数返回int型。count函数的功能是:统计容器中等于value元素的个数。
2)count_if(first,last,comp) (在comp为true的情况下计数) 或者 count_if(first,last,value,comp) (这个是在comp为true的情况下统计容器中等于value的元素):first为首迭代器,last为末迭代器,value为要查询的元素,comp为比较bool函数,为true则计数,函数返回型是int。
注:此两个函数复杂度是线性的,适用于小规模运算。count_if更加灵活
2.使用自定义谓词和绑定器《重点》
template<typename T>
void ShowEle(
const T& t)
//用于打印容器数据
{
cout << t <<
" ";
}
template<typename T>
class Mygreater :
public binary_function<T, T, bool> //1.自定义谓词需要继承binary_function
{
public:
bool operator() (
const T& iLeft,
const T& iRight)
const //2.重载()函数需要加上const,变为常函数
{
return (iLeft > iRight);
//如果是实现less<int>的话,这边是写return (iLeft<iRight);
}
};
int main()
{
vector<
int>
v1, v2, v3;
for (
int i =
0; i <
10;i++
)
v1.push_back(rand() %
30);
//v1数据插入
//打印数据
for_each(v1.begin(), v1.end(), ShowEle<
int>
);
cout <<
endl;
int num = count_if(v1.begin(), v1.end(), bind2nd(Mygreater<
int>(),
2));
cout << num <<
endl;
system("pause");
return 0;
}
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);
}
};
参照预定义函数greater
3.使用自定义二元函数对象和绑定器《重点》
template<typename T>
void ShowEle(
const T& t)
//用于打印容器数据
{
cout << t <<
" ";
}
//自定义二元函数对象---数据相加
template<typename T>
class MySumAdd :
public binary_function<T, T, int>
{
public:
int operator()(const T& t1, const T& t2) const
{
return t1 + t2;
}
};
int main()
{
vector<
int>
v1, v2, v3;
for (
int i =
0; i <
10;i++
)
v1.push_back(rand() %
30);
//v1数据插入
//打印数据
for_each(v1.begin(), v1.end(), ShowEle<
int>
);
cout <<
endl;
v2.resize(10);
//将v1中所有数据加2
transform(v1.begin(), v1.end(), v2.begin(), bind2nd(MySumAdd<int>(), 2));
//打印数据
for_each(v2.begin(), v2.end(), ShowEle<
int>
);
cout <<
endl;
system("pause");
return 0;
}
总之:自定义的仿函数和函数配接器搭配使用时,要继承自template <...> unary_function or template <...> binary_function
unary_function可以作为一个一元函数对象的基类,他定义了两个模板参数,分别是函数参数类型argument_type和返回值类型result_type,本身并不重载函数符(),由派生类去完成()操作符的重载工作。
binary_function可以作为一个二元函数对象的基类,他定义了三个模板参数,两个函数参数类型first_argument_type和second_argument_type,以及返回值类型result_type,本身并不重载函数符(),由派生类去完成()操作符的重载工作
转载于:https://www.cnblogs.com/ssyfj/p/10794574.html
相关资源:各显卡算力对照表!