c++之STL(9)重载函数调用操作符 函数对象 谓词

重载函数调用操作符

函数对象:定义了调用操作符的类,其对象称为“函数对象”

如果函数对象的返回值是bool 类型的,那么我们就称为它为谓词


一元函数对象 :函数返回一个值

一元谓词 :若返回值有一个对象,并且为bool类型的,

二元函数:对象函数对象有两个参数 

二元谓词:函数返回为bool


#include<iostream>
//
#include<vector>
#include<list>
#include<algorithm>


using namespace std;

//class absInt{
//
//public:
//	int operator()(int val){
//	
//	}
//};

// struct 里默认是公用的
struct absInt{
	// 重载操作符:函数调用操作符
	int operator()(int val){
		return val < 0 ? -val : val;
	}
};

// 普通函数
template<typename elementType>
void FuncDisplayElement(const elementType &element)
{
	cout << element << endl;
};

// 函数对象 类是有数据成员的,那么就可以保持状态
template<typename elementType>
struct DisplayElement
{
	void operator()(const elementType &element) const
	{
		cout << element << endl;
	}
};

int main()
{
	int i = -42;
	absInt absObj;
	unsigned int ui = absObj(i);
	cout << ui << endl;
	cout << "Hello 函数对象!" << endl;
	
	vector<int> a;
	for (int i = 0; i < 10; i++)
	{
		a.push_back(i);
	}
	list<char> b;
	for (char c = 'a'; c < 'k'; ++c)
	{
		b.push_back(c);
	}

	// STL算法
	for_each(a.begin(), a.end(), DisplayElement<int>());
	cout << endl;
	for_each(b.begin(), b.end(), DisplayElement<char>());
	cout << endl;
	//
	system("pause");
	return 0;
}
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

// 函数对象
template<typename numberType>
struct  IsMutiple// 默认是公认的
{
	numberType m_Divisor;
	IsMutiple(const numberType &divisor)
	{
		m_Divisor = divisor;
	}
	bool operator()(const numberType &element)const
	{
		return((element % m_Divisor) == 0);
	}
};

int main()
{
	IsMutiple<int> a(4);

	vector<int> vecIntergers;
	for (int i = 25; i < 100; i++)
	{
		vecIntergers.push_back(i);
	}
	vector<int>::iterator iElement;
	iElement = find_if(vecIntergers.begin(), vecIntergers.end(), a);
	if (iElement != vecIntergers.end())
	{
		cout << "第一个4的整数倍的数是:" <<  *iElement<< endl;
	}
	//
	system("pause");
	return 0;
}



#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

// 函数对象 一元谓词
template<typename numberType>
struct  IsMutiple// 默认是公认的
{
	numberType m_Divisor;
	IsMutiple(const numberType &divisor)
	{
		m_Divisor = divisor;
	}
	bool operator()(const numberType &element)const
	{
		return((element % m_Divisor) == 0);
	}
};

int main()
{
	//IsMutiple<int> a(4);

	vector<int> vecIntergers;
	for (int i = 25; i < 100; i++)
	{
		vecIntergers.push_back(i);
	}
	vector<int>::iterator iElement;
	iElement = find_if(vecIntergers.begin(), vecIntergers.end(), IsMutiple<int>(4));
	if (iElement != vecIntergers.end())
	{
		cout << "第一个4的整数倍的数是:" <<  *iElement<< endl;
	}
	//
	system("pause");
	return 0;
}



二元函数对象和二元谓词

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

//二元函数对象  这个不是谓词,只有返回值是bool,才被称为谓词
template<typename elementType>
class CMultiply
{
public:
	elementType operator()(const elementType &elem1, const elementType &elem2)
	{
		return elem1 * elem2;
	}
};

int main()
{
	vector<int> a, b;
	for (int i = 0; i < 10; i++)
		a.push_back(i);
	for (int j = 100; j < 110; j++)
		b.push_back(j);

	vector<int> vecResult;
	vecResult.resize(10);	// 设置大小10个
	// STL 算法
	transform(a.begin(), a.end(), b.begin(), vecResult.begin(), CMultiply<int>());

	// 对向量进行循环
	for (size_t nIndex = 0; nIndex < vecResult.size(); nIndex++)
	{
		cout << vecResult[nIndex] << endl;
	}
	//
	system("pause");
	return 0;
}
#include<iostream>
#include<set>
#include<string>
#include<algorithm>

using namespace std;

// 二元谓词 改变容器默认的行为
class CCompareStringNoCase
{
public:
	bool operator()(const string & str1, const string &str2)const
	{
		string str1LowerCase;
		str1LowerCase.resize(str1.size());
		transform(str1.begin(), str1.end(), str1LowerCase.begin(), tolower);
		//
		string str2LowerCase;
		str2LowerCase.resize(str2.size());
		transform(str2.begin(), str2.end(), str2LowerCase.begin(), tolower);

		return(str1LowerCase < str2LowerCase);
	}
};


int main()
{
	set<string> xm;
	set<string, CCompareStringNoCase> names;// 不分大小写,自己定义的一个set 容器
	names.insert("Tina");
	names.insert("jim");
	names.insert("Jack");
	names.insert("Sam");
	names.insert("hello");

	set<string>::iterator iNameFound = names.find("jim");
	if (iNameFound != names.end())
	{
		cout << "找到了:" << *iNameFound << endl;
	}
	else
	{
		cout << "" << endl; 
	}

	//
	system("pause");
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值