《Essential C++》系列笔记之第三章(泛型编程风格)之第六节(如何设计一个泛型算法)

《Essential C++》系列笔记之第三章(泛型编程风格)之第六节(如何设计一个泛型算法)_第1张图片
代码实践

#include 
using namespace std;
#include 
#include 
#include 

vector<int> demo_1_less_than_10(const vector<int> &ivec)
{
	vector<int> ret_ivec;
	for (int i = 0; i < ivec.size(); i++)
	{
		if (ivec[i] < 10)
		{
			ret_ivec.push_back(ivec[i]);
		}
	}
	return ret_ivec;
}

vector<int> demo_2_less_than(const vector<int>& ivec, const int less_than_value)
{
	vector<int> ret_ivec;
	for (int i = 0; i < ivec.size(); i++)
	{
		if (ivec[i] < less_than_value)
		{
			ret_ivec.push_back(ivec[i]);
		}
	}
	return ret_ivec;
}

inline bool greater_than(int val1, int val2)
{
	return (val1 > val2 ? true : false);
}
inline bool less_than(int val1, int val2)
{
	return (val1 < val2 ? true : false);
}
vector<int> demo_3_filter(const vector<int>& ivec, const int filter_value, bool (*p)(int, int))
{
	vector<int> ret_ivec;
	for (int i = 0; i < ivec.size(); i++)
	{
		if (p(ivec[i], filter_value))
		{
			ret_ivec.push_back(ivec[i]);
		}
	}
	return ret_ivec;
}

int demo_4_count_occurs(const vector<int> &ivec, int value)
{
	int count = 0;

	vector<int>::const_iterator iter = ivec.begin();
	while ((iter = find(iter, ivec.end(), value)) != ivec.end())
	{
		iter++;
		count++;
	}
	return count;
}

void demo_5_sort_down(vector<int> & ivec)
{
	//sort(ivec.begin(), ivec.end(), greater_than); //这样写也可以,不用包含头文件
	sort(ivec.begin(), ivec.end(), greater<int>()); //这样写就必须包含头文件 functional
}

bool demo_6_binary_search(vector<int> & ivec, int value)
{
	return (binary_search(ivec.begin(), ivec.end(), value, less<int>()));
}

vector<int> demo_7_filter(const vector<int> &vec, int val, less<int> &it)
{
	vector<int> nvec;
	vector<int>::const_iterator iter = vec.begin();

	while ( (iter = find_if(iter, vec.end(), bind2nd(it, val))) != vec.end() )
	{
		nvec.push_back(*iter);
		iter++;
	}
	return nvec;
}

template<typename InPutIterator, typename OutPutIterator, typename ElemType, typename Comp>
OutPutIterator demo_8_filter(InPutIterator first, InPutIterator last, OutPutIterator at, const ElemType &val, Comp pred)
{
	while ( (first = find_if(first, last, bind2nd(pred, val)  )) != last)  //not1(bind2nd(pred, val))可以一元取反,not2二元取反
	{
		//观察情况
		//cout << "found value: " << *first << endl;

		*at = *first;
		at++;
		first++;
	}
	return at;
}

vector<int> demo_demo_9_sub_vec(const vector<int> &ivec, int val)
{
	vector<int> local_ivec(ivec);
	sort(local_ivec.begin(), local_ivec.end());
	vector<int>::iterator iter = find_if(local_ivec.begin(), local_ivec.end(), bind2nd(greater_equal<int>(), val));
	local_ivec.erase(iter, local_ivec.end()); //虽然erase不支持偏移操作,但是可以这样做

	return local_ivec;
}

template<typename InPutIterator, typename OutPutIterator, typename ElemType, typename Comp>
OutPutIterator sub_vec(InPutIterator first, InPutIterator last, OutPutIterator at, const ElemType& val, Comp pred)
{
	//把first与last之间的值赋给at
	int n = 0;
	//OutPutIterator at_begin = at;
	while (first != last)
	{
		*at++ = *first++;
		n++;
	}
	OutPutIterator at_end = at;
	sort(at - n, at);  //!!!特别注意!!!:  这里的范围容易写成at , at + n
	//sort(at_begin, at_end);

	return find_if(at - n, at, bind2nd(pred, val));   //bind1st(pred, val)
	//return find_if(at_begin, at_end, bind2nd(pred, val));
}

int main()
{

	//demo_1_less_than_10
#if 0
	int a[] = { 1,10,9,5,23 };
	vector<int> ivec_a(a, a + 5);
	vector<int> ivec = demo_1_less_than_10(ivec_a);

	cout << ivec.size() << endl;

	for (int i = 0; i < ivec.size(); i++)
	{
		cout << ivec[i] << ' ';
	}
	cout << endl;
#endif

	//demo_2_less_than
#if 0
	int a[] = { 1,10,9,5,23 };
	vector<int> ivec_a(a, a + 5);
	vector<int> ivec = demo_2_less_than(ivec_a, 23);

	cout << ivec.size() << endl;

	for (int i = 0; i < ivec.size(); i++)
	{
		cout << ivec[i] << ' ';
	}
	cout << endl;
#endif

	//demo_3_filter
#if 0
	int a[] = { 1,10,9,5,23 };
	vector<int> ivec_a(a, a + 5);
	vector<int> ivec = demo_3_filter(ivec_a, 10, greater_than);

	cout << ivec.size() << endl;

	for (int i = 0; i < ivec.size(); i++)
	{
		cout << ivec[i] << ' ';
	}
	cout << endl;
#endif

	//demo_4_count_occurs
#if 0
	int a[] = { 1,10,10,5,23 };
	vector<int> ivec_a(a, a + 5);

	cout << demo_4_count_occurs(ivec_a, 10) << endl;

#endif

	//demo5_sort_down
#if 0
	int a[] = { 1,10,10,5,23 };
	vector<int> ivec_a(a, a + 5);

	demo_5_sort_down(ivec_a);

	for (int i = 0; i < ivec_a.size(); i++)
	{
		cout << ivec_a[i] << ' ';
	}
	cout << endl;

#endif

	//demo6_binary_search
#if 0
	int a[] = { 1,10,11,5,23 };
	vector<int> ivec_a(a, a + 5);

	//cout << binary_search(ivec_a.begin(), ivec_a.end(), 5); //会出错,因为必须是有序序列才可以使用二分查找

	//cout << demo_6_binary_search(ivec_a, 5) << endl;
	// sort(ivec_a.begin(), ivec_a.end());
	cout << binary_search(ivec_a.begin(), ivec_a.end(), 5, greater<int>()); //与书中有些不同,目前不清楚为什么

#endif

	//demo_8_filter
#if 0
	int a[] = { 1,2,3,4,5 };
	vector<int> ivec(a, a + 5);

	int a2[5] /*= { 0 }*/;
	vector<int> ivec2(5);

	/*demo_8_filter(a, a + 5, a2, 3, less());
	cout << a2[0] << endl;
	cout << a2[1] << endl;
	cout << a2[2] << endl;
	cout << a2[3] << endl;*/
	
	demo_8_filter(ivec.begin(), ivec.end(), ivec2.begin(), 3, greater<int>());
	/*cout << ivec2[0] << endl;
	cout << ivec2[1] << endl;
	cout << ivec2[2] << endl;*/

#endif

	//demo_9_sub_vec
#if 0
	int a[5] = { 23,6543,21,34,56 };
	vector<int> ivec(a, a + 5);

	vector<int> sub = demo_demo_9_sub_vec(ivec, 34);

	for (int i = 0; i < sub.size(); i++)
	{
		cout << sub[i] << ' ';
	}
	cout << endl;
#endif

	//课后练习:设计sub泛型算法
#if 1
	int a[5] = { 1,34,3,43,5 };
	vector<int> ivec(a, a + 5);

	int a2[5];
	vector<int> ivec2(5);
	
	/*int* pend = sub_vec(a, a + 5, a2, 5, greater());
	for (int* p = a2; p < pend; p++)
	{
		cout << *p << ' ';
	}
	cout << endl;*/

	vector<int>::iterator iterend = sub_vec(ivec.begin(), ivec.end(), ivec2.begin(), 5, greater<int>());
	for (vector<int>::iterator iterbeging = ivec2.begin(); iterbeging < iterend; iterbeging++)
	{
		cout << *iterbeging << ' ';
	}
	cout << endl;
#endif


	system("pause");
	return 0;
}

今天是20200311 果然是细节决定成败!

你可能感兴趣的:(《Essential,C++》系列笔记)