c++泛型算法,函数对象

目录

泛型算法

1.copy:

 2.find:

3.sort()(用到函数对象)//从小到大排

4.greater()//从大到小排

5.find_if

函数对象(和泛型算法结合使用)

1.less

2.greter

3.binder1st

4.binder2nd


泛型算法

使用到的头文件

#include<algorithm>
#include<functional>

1.copy:

实现对IT类型中对象的拷贝,第三个参数使用了插入型迭代器

template<typename IT,typename INSERT_IT>
void mcopy(const IT& first, const IT& last, INSERT_IT insert_it)//   [  )
{
	IT tmp = first;
	while (tmp != last)
	{
		*insert_it = *tmp;
		tmp++;
	}
}
int main()//调用示例
{
	vector<int> v1;
	for (int i = 10; i > 0; i--)
	{
		v1.push_back(i);
	}
	mcopy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));//将v1中的内容拷贝到输出流进行输出
	cout << endl;

	vector<int> v2;
	mcopy(v1.begin(), v1.end(), inserter(v2, v2.begin()));//插入型迭代器函数,返回插入型迭代器
	mcopy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
	cout << endl;
    return 0;
}

 2.find:

在容器中找到需要找的数,没有则返回end即指向最后

template<typename IT,typename T>
IT mfind(const IT& first, const IT& last, const T& val)
{
	IT tmp = first;
	while (tmp != last)
	{
		if (*tmp == val)
		{
			return tmp;
		}
		tmp++;
	}

	return last;
}
int main()
{
	vector<int> v1;
	for (int i = 10; i > 0; i--)
	{
		v1.push_back(i);
	}
    vector<int>::iterator it = mfind(v1.begin(),v1.end(),5);
	if (it == v1.end())
	{
		cout << "not find" << endl;
	}
	else
	{
		cout << *it << endl;
	}
}

3.sort()(用到函数对象)//从小到大排

传入容器的迭代器使容器中的数从小到大排列(第三个参数为排序顺序)

****sort底层先调用快排,待排序列很小用堆排序****

template<typename T>
class Mless
{
public:
	bool operator()(T a, T b)
	{
		return a > b;
	}
};
template<typename T>
void mswap(T& a, T& b)
{
	T tmp = a;
	a = b;
	b = tmp;
}
template<typename IT,typename PRE>
void msort(const IT& first, const IT& last,PRE pre)
{
	for (IT tmp1 = first; tmp1 != last; tmp1++)
	{
		for (IT tmp2 = tmp1 + 1; tmp2 != last; tmp2++)
		{
			//if (!(*tmp1 < *tmp2))
			if(!pre(*tmp1,*tmp2))
			{
				mswap(*tmp1, *tmp2);
			}
		}
	}
}
int main()
{
	vector<int> v1;
	for (int i = 10; i > 0; i--)
	{
		v1.push_back(i);
	}
    //从小到大排序
	sort(v1.begin(),v1.end(), Mless<int>());
	mcopy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
    return 0;
}

4.greater()//从大到小排

传入容器的迭代器使容器中的数从大到小排列(第三个参数为排序顺序)

template<typename T>
class Mgreater
{
public:
	bool operator()(T a, T b)
	{
		return a < b;
	}
};
template<typename T>
void mswap(T& a, T& b)
{
	T tmp = a;
	a = b;
	b = tmp;
}
template<typename IT,typename PRE>
void msort(const IT& first, const IT& last,PRE pre)
{
	for (IT tmp1 = first; tmp1 != last; tmp1++)
	{
		for (IT tmp2 = tmp1 + 1; tmp2 != last; tmp2++)
		{
			//if (!(*tmp1 < *tmp2))
			if(!pre(*tmp1,*tmp2))
			{
				mswap(*tmp1, *tmp2);
			}
		}
	}
}
int main()
{
	vector<int> v1;
	for (int i = 10; i > 0; i--)
	{
		v1.push_back(i);
	}
    //从小到大排序
	sort(v1.begin(),v1.end(), Mgreater<int>());
	mcopy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
    return 0;
}

5.find_if

找到传入容器中的迭代器的数中第一个比**大的或小的

template<typename T>
class Mless
{
public:
	typedef T TYPE_FIRST;
	typedef T TYPE_SECOND;
	typedef bool TYPE_RET;
	bool operator()(T a, T b)
	{
		return a < b;
	}
};
template<typename PRE>
class Mbinder2nd
{
public:
	Mbinder2nd(PRE pre,typename PRE::TYPE_SECOND val)
		:_pre(pre),_val(val)
	{
	}
	typename PRE::TYPE_RET operator()(typename PRE::TYPE_FIRST tmp)
	{
		return _pre(tmp,_val);
	}

private:
	PRE _pre;
	typename PRE::TYPE_SECOND _val;
};

template<typename IT, typename PRE>
IT mfind_if(const IT& first, const IT& last, PRE pre)
{
	IT tmp = first;
	while (tmp != last)
	{
		if(pre(*tmp))
		{
			return tmp;
		}
		tmp++;
	}

	return last;
}
int main()
{
	vector<int> v1;
	for (int i = 10; i > 0; i--)
	{
		v1.push_back(i);
	}
    vector<int>::iterator it1 = mfind_if(v1.begin(), v1.end(),Mbinder2nd<Mless<int>>(Mless<int>(),2));
	vector<int>::iterator it1 = mfind_if(v1.begin(), v1.end(), bind2nd(less<int>(), 2));//用了封装binder2nd函数返回所需类型
	if (it1 == v1.end())
	{
		cout << "not find" << endl;
	}
	else
	{
		cout << *it1 << endl;
	}
}

函数对象(和泛型算法结合使用)

1.less

template<typename T>
bool mless(T a, T b)
{
	return a < b;
}

2.greter

template<typename T>
class Mgreater
{
public:
	bool operator()(T a, T b)
	{
		return a < b;
	}
};

3.binder1st

template<typename T>
class Mless
{
public:
	typedef T TYPE_FIRST;
	typedef T TYPE_SECOND;
	typedef bool TYPE_RET;
	bool operator()(T a, T b)
	{
		return a < b;
	}
};
template<typename PRE>
class mbinder1st
{
public:
	mbinder1st(PRE pre, PRE::TYPE_FIRST val)
		:_pre(pre), _val(val)
	{

	}
	typename PRE::TYPE_PRE operator()(PRE::TYPE_SECOND tmp)
	{
		return _pre(_val,tmp);
	}
private:
	PRE _pre;
	PRE::TYPE_FIRST _val;
};

4.binder2nd

例:binder2nd 绑定第二参数binder2nd(less<int>(),10)将10和less<int>()绑定)

template<typename T>
class Mless
{
public:
	typedef T TYPE_FIRST;
	typedef T TYPE_SECOND;
	typedef bool TYPE_RET;
	bool operator()(T a, T b)
	{
		return a < b;
	}
};
template<typename PRE>
class mbinder2nd
{
public:
	mbinder2nd(PRE pre, PRE::TYPE_SECOND val)
		:_pre(pre),_val(val)
	{

	}
	typename PRE::TYPE_PRE operator()(PRE::TYPE_FIRST tmp)
	{
		return _pre(tmp, _val);
	}
private:
	PRE _pre;
	PRE::TYPE_SECOND _val;
};

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值