#include "pch.h"
#include <iostream>
#include <vector>
#include <map>
#include <functional>
#include <algorithm>
#include <ctime>
#include <string>
using namespace std;
#if 0
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;
map<int, function<void()>> actionMap;
actionMap.insert({ 1, doShowAllBooks });
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);
if (it == actionMap.end())
{
cout << "输入数字无效,重新选择!" << endl;
}
else
{
it->second();
}
}
return 0;
}
void hello1()
{
cout << "hello world!" << endl;
}
void hello2(string str)
{
cout << str << endl;
}
int sum(int a, int b)
{
return a + b;
}
class Test
{
public:
void hello(string str) { cout << str << endl; }
};
int main()
{
function<void()> func1 = hello1;
func1();
function<void(string)> func2 = hello2;
func2("hello hello2!");
function<int(int, int)> func3 = sum;
cout<<func3(20, 30)<<endl;
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