C++函数重载/运算符重载/函数对象/谓词/内建函数对象

在这里插入图片描述

加号运算符重载

在这里插入图片描述
在这里插入图片描述
我们可以通过自己写成员函数来实现加号运算符的重载。
在这里插入图片描述
但是这样呢,咱们写的函数名可能不同。这样就不统一了,所以的编译器就给我们取了一个通用的名字:operator+(oper-操作 + ator-表人 -> 操作数)。
编译器还给咱们一个小礼物,我们可以不用调用这个函数了,直接用 + (当然这只是简化了,肯定调用了我们写的函数)

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// 加号运算符重载

class Person
{
public:
	// 1.成员函数重载 + 号
	/*Person operator+(Person& p)
	{
		Person temp;
		temp.a = this->a + p.a;
		temp.b = this->b + p.b;
		return temp;
	}*/
	int a;
	int b;
};

// 2.全局函数重载 + 号
Person operator+(Person& p1, Person& p2)
{
	Person temp;
	temp.a = p1.a + p2.a;
	temp.b = p1.b + p2.b;

	return temp;
}

// 函数重载的版本
// 1.同一个作用域下
// 2.函数名相同
// 3.参数不同
Person operator+(Person& p, int n)
{
	Person temp;
	temp.a = p.a + n;
	temp.b = p.b + n;

	return temp;
}

int main()
{
	Person p1, p2;
	p1.a = 10;
	p1.b = 10;

	p2.a = 20;
	p2.b = 20;

	// 成员函数重载的本质调用
	// Person p3 = p1.operator+(p2);
	
	// 全局函数重载的本质调用
	// Person p3 = operator+(p1, p2);

	// Person p3 = p1 + p2;

	// 运算符重载是用函数来实现的
	// 为了适用多种不同的情况,运算符重载可以进行函数重载
	Person p3 = p1 + 9;

	printf("%d %d\n", p3.a, p3.b);

	return 0;
}

输出运算符重载

关系运算符重载

在这里插入图片描述
在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Person {
public:
	// 构造器
	Person(string name, int age)
	{
		this->name = name;
		this->age = age;
	}

	// 重载==号
	bool operator== (Person& p)
	{
		return this->age == p.age && this->name == p.name;
	}
	
	bool operator!= (Person& p) {
		return this->age != p.age || this->name != p.name;
	}
	
	string name;
	int age;
};

void Test01()
{
	Person p1("Tom", 18);
	Person p2("Tom", 18);

	if (p1 == p2) {
		printf("equal\n");
	}
	else {
		cout << "not equal" << endl;
	}

	if (p1 != p2) {
		cout << "not equal" << endl;
	}
	else {
		printf("equal\n");
	}
}

int main()
{
	Test01();

	return 0;
}

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <functional>  // function-函数 + al-...的,函数的,里面包含函数对象的类模版
#include <algorithm>

using namespace std;

// 定义一个结构体
struct Rec
{
	int x;
	int y;

	bool operator< (const Rec& t) const {
		return x < t.x;
	}
}a[5];  // 顺便创建一个结构体数组

//bool cmp(Rec a, Rec b)
//{
//	return a.x < b.x;
//}

int main()
{
	for (int i = 0; i < 5; ++i)
	{
		a[i].x = -i;
		a[i].y = i;
	}
	sort(a, a + 5);
	for (int i = 0; i < 5; ++i) {
		printf("x = %d, y = %d\n", a[i].x, a[i].y);
	}

	return 0;
}

函数调用运算符重载

在这里插入图片描述

匿名对象

类名()
此法没有写对象名,后面直接写了个(),调用了无参构造器,然后我们可以访问类中的成员了。


#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class myPrint
{
public:
	void operator() (string test)
	{
		cout << test << endl;
	}
};

void func(string s)
{
	cout << s << endl;
}

class myAdd
{
public:
	// 仿函数非常灵活,没有固定的写法
	int operator() (int a, int b)
	{
		return a + b;
	}
};

int main()
{
	myPrint m;
	m("13423");  // ()运算符重载,由于看起来像函数调用,因此得名“仿函数”
	func("13423");  // 函数调用

	myAdd myadd;
	printf("%d\n", myadd(3, 4));

	// 匿名函数对象
	cout << myAdd()(3, 4) << endl;

	return 0;
}

函数对象

概念1:重载 ()操作符 的类,其对象称为:函数对象(因为这时对象的名称像个函数名)
概念2:函数对象使用重载的()时,行为类似于函数调用,也叫:仿函数

在这里插入图片描述


在这里插入图片描述


谓词

在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <functional>  // function-函数 + al-...的,函数的,里面包含函数对象的类模版
#include <algorithm>

using namespace std;

class GreaterFive  // great-a.大的 + er-表比较 -> 更大的
{
	bool operator() (int val)  // 重载()时接收一个参数:一元谓词
	{
		return val > 5;
	}
};

// 仿函数的返回值类型为bool类型,称为谓词
void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; ++i)
	{
		v.push_back(i);
	}
	find_if(v.begin(), v.end(), GreaterFive());
}

int main()
{
	test01();

	return 0;
}

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <functional>  // function-函数 + al-...的,函数的,里面包含函数对象的类模版
#include <algorithm>

using namespace std;

class MyCompare
{
public:
	bool operator() (int a, int b)
	{
		return a > b;
	}
};

void test01()
{
	vector<int> v{ 50, 30, 40, 10, 20 };
	sort(v.begin(), v.end());  // 默认是升序
	for (int i = 0; i < v.size(); ++i)
	{
		printf("%d ", v[i]);
	}
	cout << endl;
	// sort第3个参数是一个匿名对象,这个就相当于一个函数名,
	// 然后只要在类中重载一下()运算符,看起来就和函数一样了
	sort(v.begin(), v.end(), MyCompare());  
	for (int i = 0; i < v.size(); ++i)
	{
		printf("%d ", v[i]);
	}
}

int main()
{
	test01();

	return 0;
}

内建函数对象

在这里插入图片描述
在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>  // function-函数 + al-...的,函数的,里面包含函数对象的类模版

using namespace std;

// 内建函数对象:算术仿函数

void test01()
{
	// 一元仿函数:negate(neg-否定 + ate-表动词 -> vt.否定) 取反仿函数
	negate<int> n;
	cout << n(50) << endl;
}

void test02()
{
	// 二元仿函数:plus 加法仿函数
	plus<int> p;
	cout << p(10, 50) << endl;
}

int main()
{
	// test01();
	test02();

	return 0;
}

在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <functional>  // function-函数 + al-...的,函数的,里面包含函数对象的类模版
#include <algorithm>

using namespace std;

// 内建函数对象:关系仿函数

class MyCompare
{
public:
	bool operator() (int a, int b)  // 不写访问修饰符默认是private,这样能避免你忘写
	{
		return a > b;
	}
};

void test01()
{
	vector<int> v{ 10, 50, 40, 30, 20 };  // 这个是不会自动排序的,set和map会
	for (vector<int>::iterator i = v.begin(); i != v.end(); ++i) {
		cout << *i << ' ';
	}
	cout << endl;
	// 降序
	// sort(v.begin(), v.end(), MyCompagreater<int>re());
	sort(v.begin(), v.end(), ());  // 内建函数对象
	for (vector<int>::iterator i = v.begin(); i != v.end(); ++i) {
		cout << *i << ' ';
	}
	cout << endl;
}

int main()
{
	test01();

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值