C++:泛型算法和函数对象

泛型算法

1.copy

#include<iostream>
#include<algorithm>
#include<functional>
#include<vector>
using namespace std;

//copy
template<typename IT,typename INSERT_IT>//顺序型迭代器&插入类型迭代器
//void mcopy(IT first, IT last, INSERT_IT 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 = 0; i < 10 ; i++)
	{
		v1.push_back(i);
	}
	mcopy(v1.begin(), v1.end(), ostream_iterator<int>(cout, "  "));
	cout << endl;

	vector<int>v2;
	mcopy(v1.begin(), v1.end(), inserter(v2, v2.begin()));//将v1数据拷贝到v2指定位置
	mcopy(v2.begin(), v2.end(), ostream_iterator<int>(cout, "  "));
	return 0;
}

在这里插入图片描述
2.find

#include<iostream>
#include<algorithm>
#include<functional>
#include<vector>
using namespace std;


//copy
template<typename IT,typename INSERT_IT>//顺序型迭代器&插入类型迭代器
//void mcopy(IT first, IT last, INSERT_IT insert_it)//前面包括数据,后面不包括
void mcopy(const IT& first, const IT& last, INSERT_IT insert_it)//前面包括数据,后面不包括
{
	IT tmp = first;
	while (tmp != last)
	{
		*insert_it = *tmp;
		tmp++;
	}
}

//find
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;
}

//swap
template<typename T>
void mswap(T& a, T& b)
{
	T tmp = a;
	a = b;
	b = tmp;
}

//less(函数对象,函数对象本质上是一个对象,使用时候同函数用法一样,因为它重载了括号运算符)
template<typename T>
class Mless
{
public:
	bool operator()(T a,T b)//重载括号运算符
	{
		cout << "bool operator()(T a,T b)" << endl;
		return a < b;
	}
};

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

//sort(可以正序逆序排序)
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 (!pre(*tmp1, *tmp2))
			{
				mswap(*tmp1, *tmp2);
			}
		}
	}
}


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, "  "));
	cout << endl;

	vector<int>v2;
	mcopy(v1.begin(), v1.end(), inserter(v2, v2.begin()));//将v1数据拷贝到v2指定位置
	mcopy(v2.begin(), v2.end(), ostream_iterator<int>(cout, "  "));
	cout << endl;

	vector<int>::iterator it = mfind(v1.begin(), v1.end(), 5);
	if (it == v1.end())
	{
		cout << "not find" << endl;
	}
	else
	{
		cout << *it << endl;
	}

	//从小到大排序
	msort(v1.begin(), v1.end(), Mless<int>());
	mcopy(v1.begin(), v1.end(), ostream_iterator<int>(cout, "  "));

	Mless<int> _less;
	_less(10, 20);


	return 0;
}

在这里插入图片描述
3.绑定器

#include<iostream>
#include<algorithm>
#include<functional>
#include<vector>
using namespace std;


//copy
template<typename IT,typename INSERT_IT>//顺序型迭代器&插入类型迭代器
//void mcopy(IT first, IT last, INSERT_IT insert_it)//前面包括数据,后面不包括
void mcopy(const IT& first, const IT& last, INSERT_IT insert_it)//前面包括数据,后面不包括
{
	IT tmp = first;
	while (tmp != last)
	{
		*insert_it = *tmp;
		tmp++;
	}
}

//find
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;
}

//查找一下第一个小于10的元素
template<typename IT, typename PRE>
IT mfind_if(const IT& first, const IT& last, PRE pre)//传入绑定器对象
{
	IT tmp = first;
	while (tmp != last)//遍历每一个数据传入到绑定器对象里面
	{
		//if (*tmp < 10)
		//if (less(*tmp, 10))
		if (pre(*tmp))
		{
			return tmp;
		}
		tmp++;
	}
	return last;
}

//绑定器:给函数对象绑定参数,
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;
};

//swap
template<typename T>
void mswap(T& a, T& b)
{
	T tmp = a;
	a = b;
	b = tmp;
}

//less(函数对象,函数对象本质上是一个对象,使用时候同函数用法一样,因为它重载了括号运算符)
template<typename T>
class Mless
{
public:
	typedef T TYPE_FIRST;
	typedef T TYPE_SECOND;
	typedef bool TYPE_RET;
	bool operator()(T a,T b)//重载括号运算符
	{
		cout << "bool operator()(T a,T b)" << endl;
		return a < b;
	}
};

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

//sort(可以正序逆序排序)
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 (!pre(*tmp1, *tmp2))
			{
				mswap(*tmp1, *tmp2);
			}
		}
	}
}


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, "  "));
	cout << endl;

	vector<int>v2;
	mcopy(v1.begin(), v1.end(), inserter(v2, v2.begin()));//将v1数据拷贝到v2指定位置
	mcopy(v2.begin(), v2.end(), ostream_iterator<int>(cout, "  "));
	cout << endl;

	vector<int>::iterator it = mfind(v1.begin(), v1.end(), 5);
	if (it == v1.end())
	{
		cout << "not find" << endl;
	}
	else
	{
		cout << *it << endl;
	}

	//从小到大排序
	msort(v1.begin(), v1.end(), Mless<int>());
	mcopy(v1.begin(), v1.end(), ostream_iterator<int>(cout, "  "));

	Mless<int> _less;
	_less(10, 20);

	vector<int>::iterator it1 = find_if(v1.begin(), v1.end(), Mbinder2nd<Mless<int>>(Mless<int>(), 10));
	if (it1 == v1.end())
	{
		cout << "not find" << endl;
	}
	else
	{
		cout << *it1 << endl;
	}

	return 0;
}

在这里插入图片描述

#include<iostream>
#include<algorithm>
#include<functional>
#include<vector>
using namespace std;


//copy
template<typename IT,typename INSERT_IT>//顺序型迭代器&插入类型迭代器
//void mcopy(IT first, IT last, INSERT_IT insert_it)//前面包括数据,后面不包括
void mcopy(const IT& first, const IT& last, INSERT_IT insert_it)//前面包括数据,后面不包括
{
	IT tmp = first;
	while (tmp != last)
	{
		*insert_it = *tmp;
		tmp++;
	}
}

//find
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;
}

//查找一下第一个小于10的元素
template<typename IT, typename PRE>
IT mfind_if(const IT& first, const IT& last, PRE pre)//传入绑定器对象
{
	IT tmp = first;
	while (tmp != last)//遍历每一个数据传入到绑定器对象里面
	{
		//if (*tmp < 10)
		//if (less(*tmp, 10))
		if (pre(*tmp))
		{
			return tmp;
		}
		tmp++;
	}
	return last;
}

//绑定器1
template<typename PRE>
class Mbinder1st
{
public:
	Mbinder1st(PRE pre, typename PRE::TYPE_FIRST val)
		:_pre(pre), _val(val)
	{

	}

	typename PRE::TYPE_RET operator()(typename PRE::TYPE_SECOND tmp)
	{
		return _pre(_val, tmp);
	}

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

//绑定器2:给函数对象绑定参数,
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;
};

//swap
template<typename T>
void mswap(T& a, T& b)
{
	T tmp = a;
	a = b;
	b = tmp;
}

//less(函数对象,函数对象本质上是一个对象,使用时候同函数用法一样,因为它重载了括号运算符)
template<typename T>
class Mless
{
public:
	typedef T TYPE_FIRST;
	typedef T TYPE_SECOND;
	typedef bool TYPE_RET;
	bool operator()(T a,T b)//重载括号运算符
	{
		cout << "bool operator()(T a,T b)" << endl;
		return a < b;
	}
};

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

//sort(可以正序逆序排序)
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 (!pre(*tmp1, *tmp2))
			{
				mswap(*tmp1, *tmp2);
			}
		}
	}
}


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, "  "));
	cout << endl;

	vector<int>v2;
	mcopy(v1.begin(), v1.end(), inserter(v2, v2.begin()));//将v1数据拷贝到v2指定位置
	mcopy(v2.begin(), v2.end(), ostream_iterator<int>(cout, "  "));
	cout << endl;

	vector<int>::iterator it = mfind(v1.begin(), v1.end(), 5);
	if (it == v1.end())
	{
		cout << "not find" << endl;
	}
	else
	{
		cout << *it << endl;
	}
	less<int>();

	//从小到大排序
	msort(v1.begin(), v1.end(), Mless<int>());
	mcopy(v1.begin(), v1.end(), ostream_iterator<int>(cout, "  "));

	Mless<int> _less;
	_less(10, 20);

	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));//用函数封装类模板


	if (it1 == v1.end())
	{
		cout << "not find" << endl;
	}
	else
	{
		cout << *it1 << endl;
	}

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值