function函数对象应用示例

news/2024/7/24 5:50:22 标签: python, java, c++, php, c#
#include "pch.h"
#include <iostream>
#include <vector> 
#include <map>
#include <functional>  // 使用function函数对象类型
#include <algorithm>
#include <ctime>
#include <string>
using namespace std;

#if 0
/*
C++11提供的绑定器和函数对象
          bind   function
		  C++ STL  bind1st和bind2nd =》 本身还是一个函数对象
function : 绑定器,函数对象,lambda表达式 它们只能使用在一条语句中
*/

void doShowAllBooks() { cout << "查看所有书籍信息" << endl; }
void doBorrow() { cout << "借书" << endl; }
void doBack() { cout << "还书" << endl; }
void doQueryBooks() { cout << "查询书籍" << endl; }
void doLoginOut() { cout << "注销" << endl; }

int main()
{
	int choice = 0;
	//       C的函数指针
	map<int, function<void()>> actionMap;
	actionMap.insert({ 1, doShowAllBooks }); // insert(make_pair(xx,xx));
	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 << "请选择:";
		cin >> choice;

		auto it = actionMap.find(choice); // map  pair  first second
		if (it == actionMap.end())
		{
			cout << "输入数字无效,重新选择!" << endl;
		}
		else
		{
			it->second();
		}
		// 不好,因为这块代码无法闭合  无法做到“开-闭原则
		/*switch (choice)
		{
		case 1:
			break;
		case 2:
			break;
		case 3:
			break;
		case 4:
			break;
		case 5:
			break;
		default:
			break;
		}*/
	}

	return 0;
}


void hello1()
{
	cout << "hello world!" << endl;
}
void hello2(string str) // void (*pfunc)(string)
{
	cout << str << endl;
}
int sum(int a, int b)
{
	return a + b;
}

class Test
{
public: // 必须依赖一个对象void (Test::*pfunc)(string)
	void hello(string str) { cout << str << endl; }
};
int main()
{
	/*
	1.用函数类型实例化function
	2.通过function调用operator()函数的时候,需要根据函数类型传入相应的参数
	*/
	// 从function的类模板定义处,看到希望用一个函数类型实例化function
	function<void()> func1 = hello1;
	func1(); // func1.operator()() => hello1()

	function<void(string)> func2 = hello2;
	func2("hello hello2!"); // func2.operator()(string str) => hello2(str)

	function<int(int, int)> func3 = sum;
	cout<<func3(20, 30)<<endl;

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

	function<void(Test*, string)> func5 = &Test::hello;
	func5(&Test(), "call Test::hello!");

	return 0;
}
#endif

http://www.niftyadmin.cn/n/923555.html

相关文章

lambda表达式实例

#include "pch.h" #include <iostream> #include <algorithm> #include <vector> #include <functional> #include <map> #include <memory> #include <queue> using namespace std;/* 既然lambda表达式只能使用在语句当中…

生产者消费者线程间通信模型

#include "pch.h" #include <iostream> #include <thread> #include <mutex> #include <condition_variable> #include <queue> // C STL所有的容器都不是线程安全 using namespace std; #if 0 /* unique_lock condition_variable 1.…

C++模拟车站窗口卖票

#include "pch.h" #include <iostream> #include <thread> #include <mutex> #include <list> using namespace std;#if 0 /* C thread 模拟车站三个窗口卖票的程序线程间的互斥 》 互斥锁mutex 》 lock_guard封装mutex */ int ticketCount …

线程安全的懒汉/饿汉单例模式的实现

#include "pch.h" #include <iostream> #include <mutex> using namespace std;/* 单例模式&#xff1a;一个类不管创建多少次对象&#xff0c;永远只能得到该类型一个对象的实例 A *p1 new A(); A *p2 new A(); A *p3 new A(); 常用到的&#xff0c;…

深度遍历迷宫搜索C++实现

// 深度遍历迷宫路径搜索.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 //#include "pch.h" #include <iostream> #include <stack> using namespace std;#if 0 /* 深度遍历搜索迷宫路径&#xff0c;软件运行要求如下&#xf…

数据结构中二叉树的度

首先说说什么是度&#xff1a;通俗的讲二叉树中连接节点和节点的线就是度&#xff0c;有n个节点&#xff0c;就有n-1个度&#xff0c;节点数总是比度要多一个&#xff0c;那么度为0的节点一定是叶子节点&#xff0c;因为该节点的下面不再有线&#xff1b;度为1的节点即&#xf…

大数的加减法

#include "pch.h" #include <iostream> #include <string> #include <algorithm> using namespace std;#if 0 // 编程题目&#xff1a;请实现以下类的方法&#xff0c;完成大数的加减法 class BigInt { public:BigInt(string str) :strDigit(str) …

C++实现LRU(最久未使用)缓存算法

LRU缓存算法也叫LRU页面置换算法&#xff0c;是一种经典常用的页面置换算法&#xff0c;本文将用C实现一个LRU算法。 LRU算法实现并不难&#xff0c;但是要高效地实现却是有难度的&#xff0c;要想高效实现其中的插入、删除、查找&#xff0c;第一想法就是红黑树&#xff0c;但…