C++-函数对象

本文通过四个实例展示了C++中STL的一元函数对象、一元谓词、二元函数对象和二元谓词的使用。这些函数对象和谓词在处理容器元素时,如vector和list,以及在算法如find_if和transform中的应用,体现了其在编程中的灵活性和效率。同时,还展示了如何自定义函数对象和谓词以满足特定需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一元函数对象:

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

//一元函数对象,可以作为STL函数的参数
struct absInt {
	int operator() (int val) {	//一元函数操作符
		return val < 0 ? -val : val;
	}
};
//模板函数
template<typename elementType>
void FuncDispalyElement(const elementType& element)
{
	cout << element << ' ';
}
//模板类,可以通过成员变量保持状态
template<typename elementType>
struct DisPlayElement
{
	int m_nCount;
	DisPlayElement()
	{
		m_nCount = 0;
	}
	void operator() (const elementType& element)
	{
		++m_nCount;
		cout << element << ' ';
	}
};
int main()
{
	int i = -42;
	absInt absObj;
	unsigned int ui = absObj(i);
	cout << ui << endl;
	cout << "Hello 函数对象!" << endl;

	vector<int> a;
	for (int n = 0; n < 10; ++n)
		a.push_back(n);

	list<char> b;
	for (char c = 'a'; c < 'k'; ++c)
		b.push_back(c);

	//STL算法
	DisPlayElement<int> mResult;
	mResult = for_each(a.begin(), a.end(), /*匿名函数对象DisPlayElement<int>()*/mResult);
	cout << endl;
	cout << "数量:" << mResult.m_nCount << endl;

	for_each(b.begin(), b.end(), DisPlayElement<char>());
	cout << endl;

	return 0;
}

一元谓词:

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

//模板类,可以通过成员变量保持状态
template<typename numberType>
struct IsMutiple
{
	numberType m_Divisor;
	IsMutiple(const numberType& divisor)
	{
		m_Divisor = divisor;
	}
	void operator() (const numberType& element) const
	{
		return ((element%m_Divisor)==0);
	}
};
int main()
{
	vector<int> vecIntegers;
	for (int i = 25; i < 100; ++i)
		vecIntegers.push_back(i);
	
	IsMutiple<int> a(4);
	vector<int>::iterator iElement;
	iElement=find_if(vecIntegers.begin(),vecIntegers.end(),a/*IsMutiple<int>(4)*/);
	if(iElement!=vecIntegers.end())
	{
		cout<<"第一个4的整数倍的数是:"<<*iElement<<endl;
	}

	return 0;
}

二元函数对象:

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


//模板类,可以通过成员变量保持状态
template<typename elementType>
class CMultiply
{
public:
	elementType operator() (const elementType& elem1, 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=0;j<10;++j)
		b.push_back(j);
	
	vector<int> vecResult;
	vecResult.resize(10);
	
	transform(a.begin(),a.end(),b.begin(),vecResult.begin(),CMultiply<int>());
	
	for(size_t nIndex=0;nIndex<vecResult.size();++nIndex)
		cout<<vecResult[nIndex]<<' ';
	cout<<endl;

	return 0;
}

二元谓词:

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


class CCompareStringNoCase
{
public:
	bool operator() (const string& str1, const string& str2) const	//这里的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,CCompareStringNoCase> names;
	names.insert("Tina");
	names.insert("Jim");
	names.insert("Jack");
	names.insert("Sam");
	names.insert("Bob");

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

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值