C++中二元函数对象和二元谓词

在c++ STL算法中,在算法的输入和输出中,一定要分清是函数对象,还是谓词,还是迭代器。

具体看如下代码,注意看注释:

#include<iostream>
using namespace std;
#include "string"
#include <vector>
#include <list>
#include <set>
#include <algorithm>
#include <functional>


//二元函数对象
template<typename T>
class SumAdd
{
public:
	T operator()(T t1, T t2)
	{
		return t1 + t2;
	}
};

//函数模板  ==函数
template <typename T>
void FuncShowElemt(T& t)
{
	cout << t << endl;
}

//普通函数
void FuncShowElemt2(int& t)
{
	cout << t << " ";
}


//二元谓词
template<typename T>
bool Mycompare(const T& a, const T& b)
{
	return a < b;
}



void main04()
{
	vector<int> v1, v2;
	vector<int> v3;
	v1.push_back(1);
	v1.push_back(3);
	v1.push_back(5);

	v2.push_back(2);
	v2.push_back(4);
	v2.push_back(6);
	v3.resize(10);

	/*
	template<class _InIt1,
	class _InIt2,
	class _OutIt,
	class _Fn2> inline
		_OutIt transform(_InIt1 _First1, _InIt1 _Last1,
		_InIt2 _First2, _OutIt _Dest, _Fn2 _Func)
	{	// transform [_First1, _Last1) and [_First2, ...) with _Func
		_DEBUG_RANGE(_First1, _Last1);
		_DEBUG_POINTER(_Dest);
		_DEBUG_POINTER(_Func);
		if (_First1 != _Last1)
			return (_Transform2(_Unchecked(_First1), _Unchecked(_Last1),
			_First2, _Dest, _Func,
			_Is_checked(_Dest)));
		return (_Dest);
	}
	//transform 把运算结果的 迭代器的开始位置 返回出来
	*/


	transform(v1.begin(),v1.end(),v2.begin(),v3.begin(),SumAdd<int>());

	for (vector<int>::iterator it = v3.begin(); it != v3.end(); it++)
	{
		cout << *it << endl;
		// 输出: 3 7 11 0  0  0  0  0  0  0
	}
	cout << endl;
}


void main05()
{
	vector<int> v1(10);

	for (int i = 0; i < 10; i++)
	{
		int tmp = rand() % 100;
		v1[i] = tmp;
	}

	for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
	{
		cout << *it << " ";    //输出: 41 67 34 0 69 24 78 58 62 64
	}
	cout << endl;
	for_each(v1.begin(),v1.end(),FuncShowElemt2);  //输出:41 67 34 0 69 24 78 58 62 64
	cout << endl;

	sort(v1.begin(),v1.end(),Mycompare<int>);   
	for_each(v1.begin(),v1.end(),FuncShowElemt2);  //输出: 0 24 34 41 58 62 64 67 69 78
	cout << endl;
}

class CompareNoCase
{
public:
	bool operator()(const string& str1, const string& str2) const
	{
		string str1_;
		str1_.resize(str1.size());
		transform(str1.begin(), str1.end(), str1_.begin(), tolower); //预定义函数对象 

		string str2_;
		str2_.resize(str2.size());
		transform(str2.begin(), str2.end(), str2_.begin(), tolower); //预定义函数对象 
		return (str1_ < str2_); // 从小到大进行排序
	}
};
void  main06()
{
	set<string> set1;
	set1.insert("bbb");
	set1.insert("aaa");
	set1.insert("ccc");
	set<string>::iterator it = set1.find("aAa"); //find函数 默认 区分大小写
	if (it == set1.end())
	{
		cout << " 没有 查找到 aaa " << endl;
	}
	else
	{
		cout << " 查找到 aaa " << endl;
	}

	set<string, CompareNoCase> set2;
	set2.insert("bbb");
	set2.insert("aaa");
	set2.insert("ccc");

	set<string, CompareNoCase>::iterator it2 = set2.find("aAa");
	if (it2 == set2.end())
	{
		cout << " 没有 查找到 aaa " << endl;
	}
	else
	{
		cout << " 不区分大小的的查找  查找到 aaa " << endl;
	}
	/**
	输出:
	没有 查找到 aaa
	不区分大小的的查找  查找到 aaa
	*/
}

void main()
{
	//main04(); //二元函数对象 和二元谓词
	//main05(); //二元函数对象 和二元谓词
	main06(); //二元谓词在set集合中的应用
	cout << "hello..." << endl;
	system("pause");
	return;
}
发布了293 篇原创文章 · 获赞 113 · 访问量 25万+

猜你喜欢

转载自blog.csdn.net/tianguiyuyu/article/details/105112617