泛型算法Function Objects模板化学习

泛型算法模板化


#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
template <typename InputIterator,typename OutputIterator,typename ElemType,
        typename Comp>
OutputIterator
filter(InputIterator first,InputIterator last,
        OutputIterator at,const ElemType &val,Comp pred);
        

int main()
{
    const int elem_size=8;
    int ia[elem_size]={12,8,43,0,6,21,3,7};
    vector<int> ivec(ia,ia+elem_size);
    int ia2[elem_size];
    vector<int> ivec2(elem_size);
    cout<<"filtering integer array for values less than 8\n";
    filter(ia,ia+elem_size,ia2,elem_size,less<int>());
    
    cout<<"filtering integer vector for values greater than 8\n";
    filter(ivec.begin(),ivec.end(),ivec2.begin(),elem_size,greater<int>());
    
    return 0;
}

template <typename InputIterator,typename OutputIterator,typename ElemType,
        typename Comp>
OutputIterator
filter(InputIterator first,InputIterator last,
        OutputIterator at,const ElemType &val,Comp pred)
{
    while((first=find_if(first,last,bind2nd(pred,val)))!=last)
    {
    cout<<"found value: "<<*first<<endl;
    
    *at++=*first++;
    }
    return at;
}

依照上面的模板函数,将下面的代码改写成template模式

vector<int> sub_vec(const vector<int> &vec,int val)
{
	vector<int> local_vec(vec);
	sort(local_vec.begin(),local_vec.end());
	vector<int>::iterator iter=find_if(local_vec.begin(),local_vec.end(),bind2nd(greater<int>(),val));
	local_vec.erase(iter,local_vec.end());
	return local_vec;
}

修改的难点在于不影响原先的容器,并在此基础上将需要的值返回。

初次修改的代码如下,代码有误,仅用来学习思想:

template<typename InputIterator,typename OutputIterator,typename Elemtype,typename Comp>
OutputIterator 
sub_vec(InputIterator first,InputIterator last,OutputIterator at,const Elemtype &val,Comp pred)
{
sort(first,last);
while(first!=last)
{
	*at++=*first++;
}
	OutputIterator iter=find_if(first,last,bind2nd(pred,val));
	erase(iter,at);
	return at;
}

再次修改的代码,代码正确

template<typename InputIterator,typename OutputIterator,typename Elemtype,typename Comp>
OutputIterator 
sub_vec(InputIterator first,InputIterator last,OutputIterator at,const Elemtype &val,Comp pred)
{
sort(first,last);
InputIterator iter=find_if(first,last,bind2nd(pred,val));
while(first!=iter)
{
	*at++=*first++;
}
	return at;
}

显示验证,代码错误

template<typename InputIterator> 
void display(InputIterator first)
{
    while(!first->empty())
    {
        cout<<*first++<<" ";
    }
    cout<<endl;
}

出现的错误:
error: request for member ‘empty’ in ‘* first’, which is of non-class type ‘int’

显示代码的反思

对于一个几乎完全泛型化的显示代码,知道开头和结尾几乎是必须的

template<typename InputIterator>
void display(InputIterator first,InputIterator last)
{
	for(;first!=last;first++)
	{
		cout<<*first<<' ';
	}
	cout<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值