【一文读不懂C++】Function的应用

举报
无敌清风蓝 发表于 2023/06/18 14:22:58 2023/06/18
【摘要】 Function的应用

1.function的应用

#include <iostream>
#include <vector>
#include <vector>
#include <functional>
#include <algorithm>
#include <ctime>

using namespace std;

void hello1()
{
	cout << "hello" << endl;
}

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

int sum(int a, int b)
{
	cout << a + b << endl;
	return a + b;
}

class Test
{
public:	//类的成员函数调用必须依赖一个对象void (Test::::*pfunc)(string)
	void hello(string str) { cout << str << endl; }
};
int main()
{
	// 从function的类模板定义处,看到希望用一个函数类型实例化function
	function<void()> func1 = hello1;
	func1();
	function<void(string)> func2 = hello2;
	func2("hello2");
	function<int(int, int)> func3 = sum;
	func3(1, 2);

	// operator()
	function<int(int, int)> func4 = [](int a, int b)->int {return a + b; };
	cout << func4(100, 200) << endl;

	// 因为成员函数的参数里面都有用一个指向自己的指针,也就是Test*
	function<void(Test*, string)> func5 = &Test::hello;
	// 对于成员方法的调用,是要指向一个对象的,也就是&Test()
	Test t;
	func5(&t, "hello test!");
	return 0;
}
// 总结
//1.function<函数类型>,用函数类型实例化function
//2.function<函数类型(参数)>  func1  使用时候也是得func1(参数)

2.function 的好处

那说了这么多,function的好处到底是哪里呢,为什么我要这样子干,而不是直接调用函数呢

好处就是它能把看到的各种类型保存下来

比如

func1 = hello1;

func4 = [](int a, int b)->int {return a + b; };

func5 = &Test::hello;

它能把函数,lambda表达式,成员函数的类型保存下来

举个例子,比如图书馆里系统图

int main()
{
	int choice = 0;
	for (;;)
	{
		cout << "----------------" << endl;
		cout << "1.查看所有书籍信息" << endl;
		cout << "2.借书" << endl;
		cout << "3.还书" << endl;
		cout << "4.查询书籍" << endl;
		cout << "5.注销" << endl;
		cout << "----------------" << endl;
		cout << "请选择:" << endl;
		cin >> choice;
	}
	switch (choice)
	{
	case 1:
		break;
	case 2:
		break;
	case 3:
		break;
	case 4:
		break;
	case 5:
		break;
	default:
		break;
	}

}

但是这个代码不好,因为无法闭合,无法做到开闭原则,每次新增加一个功能 都要把switch里面的都改一下

我们可以把每件事情的功能都封装到一个函数里

void doShowAllBook() { cout << "查看所有书籍信息" << endl; }
void doBorrow() { cout << "借书" << endl; }
void doBack() { cout << "还书" << endl; }
void doQueryBooks() { cout << "查询书籍" << endl; }
void doLoginOut() { cout << "注销" << endl; }
int main()
{
	int choice = 0;
	map <int, function<void()>> actionMap;
	actionMap.insert({ 1, doShowAllBook });
	actionMap.insert({ 2, doBorrow });
	actionMap.insert({ 3, doBack });
	actionMap.insert({ 4, doQueryBooks });
	actionMap.insert({ 5, doLoginOut });

	for (;;)
	{
		cout << "----------------" << endl;
		cout << "1.查看所有书籍信息" << endl;
		cout << "2.借书" << endl;
		cout << "3.还书" << endl;
		cout << "4.查询书籍" << endl;
		cout << "5.注销" << endl;
		cout << "----------------" << endl;
		cout << "请选择:" << endl;
		cin >> choice;
		auto it = actionMap.find(choice);
		if (it == actionMap.end())
		{
			cout << "failed" << endl;
		}
		else
		{
			it->second();
		}
	}
}

有人说那函数指针也可以做到呀,但是我们这里只是用了函数,那要是有lambda,有函数绑定怎么办呢,对吧,所以还是用function靠谱,可以返回任何的类型。

像lambda表达式只能作用在语句中,而有了function就可以随心所欲的用了,要不然就得重新写表达式或者重新绑定了

【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。