当前位置: 首页> 汽车> 新车 > discuz应用中心破解_动态网站制作素材_怎么去做网络推广_自助建站系统破解版

discuz应用中心破解_动态网站制作素材_怎么去做网络推广_自助建站系统破解版

时间:2025/7/11 9:25:35来源:https://blog.csdn.net/2301_79181030/article/details/142730097 浏览次数: 0次
discuz应用中心破解_动态网站制作素材_怎么去做网络推广_自助建站系统破解版

欢迎来到 破晓的历程的 博客

⛺️不负时光,不负己✈️

文章目录

    • 引言
    • function函数对象
      • function引入
      • 细讲function
      • 体验function在工程实践中的优势
    • 模拟实现function函数对象机制
    • bind绑定器
      • 基本语法
      • 示例
        • 1. 绑定普通函数
        • 2. 使用占位符
        • 3. 绑定成员函数
        • 4. 绑定 lambda 表达式
      • 注意事项
    • 使用bind+function完成线程池设计
      • 何为线程池?
      • 设计代码

引言

在C++中工程实践中,bind绑定器和function函数对象非常常用,而且bind+function简直就是无敌的存在。本篇博客中,我们力求用最小的成本搞懂它们,让你用起来得心应手

function函数对象

以减少理解成本为目地,我们先学习function。

function引入

function的作用是将具有相同调用形式的不同类型可调用对象进行类型统一。

相同的调用形式可以简单理解为:参数列表和返回值相同。

C++常见可调用对象有:函数、指针、匿名函数(lambda表达式)、函数对象(重载了函数调用运算符的类)以及使用bind创建的对象。
例如以下几个可调用对象具有相同的调用形式:

// 函数
int add(int a, int b) {return a + b;
}// lambda表达式
auto sub = [](int a, int b) -> int {return a - b;
};// 函数对象
class prod{
public:int operator() (int a, int b) {return a * b;}
};

这些可调用对象的调用形式相同,甚至拥有相同的功能,但是却由于类型千差万别无法统一处理。例如,我们想要统一调用以上可调用对象,使用‘+’来调用add函数,而使用‘-’调用sub匿名函数…
使用分支语句(if else if else)当然是可以做到的,但更推荐以下方式:

/*  使用map映射字符到可调用对象。但需要注意,映射的前提是function对不同类型的可调用对象
进行了类型统一。 */
map<char, function<int(int, int)>> cacul{{'+', add},{'-', sub},{'*', prod()}
};cout << cacul['*'](5, 4) << endl;

细讲function

funciotn是从c++11开始支持的特性,使用它需要包含头文件functional
在cppreference中解释为:类模板std::function是一个通用的多态函数包装器。std::function的实例可以对任何可以调用的目标实体进行存储、复制、和调用操作,这些目标实体包括普通函数、Lambda表达式、函数指针、以及其它函数对象等。
通俗的来说可以把它当做一个函数指针来使用

让我们来感受一下:
function的模板是 std::function<返回值类型(传入参数类型)> 方法名
这里传入参数类型可以是自己定义的

举几个简单的例子:

function<int(int, int)> func

这表示:定义了一个返回值为int,参数有两个,从左往右为int,int类型的函数指针

int sum(int a, int b)
{return a + b;
}
int main()
{using func_t = function<int(int, int)>;func_t func = sum;cout<<func(10,10);
}

这里,我创建了一个返回值为int,参数有两个,从左往右为int,int类型的函数指针。然后用该函数指针创建一个对象func,将sum「函数名表示该函数的地址」赋值给func。然后就可以通过func调用sum函数。
或者也可以这样:

int main()
{function<int(int, int)> func = sum;cout<<func(10,10);
}

用也许这样更容易被理解。

体验function在工程实践中的优势

假如我们要设计一个图书管理系统,该系统提供的服务有:借书、查询书、还书。假设这些函数的函数签名都是一样的「即返回值类型和参数类型都是相同的」。我们习惯将服务编号,比如用户输入1表示要获取借书服务,用户输入2表示查询书服务,用户输入3表示还书服务。
此时的开发者有两种设计方案「大体框架,不包括不同模块设计的具体细节」

  • 使用switch判断语句,但是这种框架的劣性在于如果增加一个模块需要进行大范围修改。
  • 使用function和map数据结构相结合。

接下来我们重点介绍第二种框架
本着说明问题,其他一切从简的原则 我们可以这样设计


#include<iostream>
#include<functional>
#include<map>
using namespace std;
void borrow()
{std::cout << "borrow books" << std::endl;
}
void lend()
{std::cout << "Return the book" << std::endl;}
void search()
{std::cout << "Search book" << std::endl;
}
int main()
{using func_t = std::function<void()>;std::map<int, func_t> Map;Map.insert({ 1,borrow });Map.insert({ 2,lend });Map.insert({ 3,search });while (1){int option = 0;std::cout << "请选择:";std::cin >> option;auto it = Map.find(option);if (it == Map.end()){cout << "没有这项服务,请重新选择:" << endl;}else{it->second();}}
}

模拟实现function函数对象机制

如上,我们系统的介绍了function的使用方法和应用,但是它究竟是如何实现的呢?我们一切来探究一下

#include<iostream>
#include<string>
template<class R,class A>
class myfunction<R(A)>
{
public:using PFUNC = R(*)A;myfunction(PFUNC pfunc):_pfunc(pfunc){}R operator()(A ter){return _pfunc(str);}
priavte:PFUNC _pfunc;};
void hello(std::string str)
{std::cout << str << std::endl;
}
int main()
{myfunction<void(std::string)> func_t = hello;func_t("hello world");
}

但是如果函数签名发生变化,我们就得写不同的部分特例化模板,其实我们可以这样做:

template<class T,class... A1>
class my_function<T(A1...)>
{
public:using func_t = T(*)(A1...);my_function(func_t func):func_(func){}R operator()(A1... args){return func_(args...);}
private:func_t func_;
};

这就是模板的强大之处。

bind绑定器

关于bind绑定器,百度百科是这样说的:
std::bind 是 C++11 引入的一个标准库函数,它位于 functional 头文件中。std::bind 可以用来绑定函数的参数,或者将成员函数和对象绑定在一起,生成一个新的可调用对象(也称为函数对象)。这个新生成的对象可以像普通函数一样被调用,但其内部实际上会调用我们最初绑定的那个函数或成员函数。

基本语法

#include <functional> // 包含 std::bind 的头文件auto bound_function = std::bind(function, arg1, arg2, ..., argN);
  • function 是要绑定的函数或可调用对象。
  • arg1, arg2, ..., argN 是传递给 function 的参数,可以是具体的值,也可以是占位符 _1, _2, ...(这些占位符定义在 <placeholders> 头文件中,通常通过 std::placeholders::_1 等方式访问)。

示例

1. 绑定普通函数
#include <iostream>
#include <functional>void print_sum(int a, int b) {std::cout << "Sum: " << a + b << std::endl;
}int main() {auto bound_print_sum = std::bind(print_sum, 5, 10);bound_print_sum(); // 输出: Sum: 15return 0;
}
2. 使用占位符
#include <iostream>
#include <functional>
#include <placeholders> // 包含 std::placeholders 的头文件void print_sum(int a, int b) {std::cout << "Sum: " << a + b << std::endl;
}int main() {using namespace std::placeholders;auto bound_print_sum = std::bind(print_sum, _1, 10);bound_print_sum(5); // 输出: Sum: 15bound_print_sum(20); // 输出: Sum: 30return 0;
}
3. 绑定成员函数
#include <iostream>
#include <functional>
#include <placeholders>class MyClass {
public:void print_sum(int a, int b) {std::cout << "Sum: " << a + b << std::endl;}
};int main() {MyClass obj;using namespace std::placeholders;auto bound_print_sum = std::bind(&MyClass::print_sum, &obj, _1, 10);bound_print_sum(5); // 输出: Sum: 15return 0;
}

注意,在绑定成员函数时,第一个参数需要是对象的指针或引用。

4. 绑定 lambda 表达式
#include <iostream>
#include <functional>
#include <placeholders>int main() {auto lambda = [](int a, int b) { std::cout << "Sum: " << a + b << std::endl; };auto bound_lambda = std::bind(lambda, _1, 20);bound_lambda(10); // 输出: Sum: 30return 0;
}

注意事项

  1. 性能std::bind 生成的函数对象在调用时,可能会引入额外的间接调用开销,因此在某些性能敏感的场景中,需要谨慎使用。
  2. 兼容性std::bind 是 C++11 引入的特性,因此确保你的编译器支持 C++11 或更高版本。
  3. 替代方案:在 C++11 及以后的版本中,lambda 表达式通常是一个更灵活和直观的选择,用于实现类似的功能。

通过上面的示例和解释,你应该对 std::bind 的基本用法有了初步的了解。在实际开发中,根据具体需求选择合适的工具和方法来实现功能。

使用bind+function完成线程池设计

何为线程池?

线程池是一种高效的设计方案。我们事先准备若干个线程,然后给不同的线程分配不同的任务。

设计代码

#include<iostream>
#include<functional>
#include<string>
#include<thread>
#include<vector>
using namespace std;
using namespace std::placeholders;
class Thread
{
public:Thread(function<void()>Way):_Way(Way){}thread start(){thread t(_Way);return t;}
private:function<void()> _Way;
};
class ThreadPoll
{
public:ThreadPoll(){}~ThreadPoll(){for (auto it : _poll){delete it;}}void start(int size){for (int i = 0; i < size; i++){_poll.push_back(new Thread(std::bind(&ThreadPoll::ThreadWay, this, i)));}for (auto it : _poll){_hander.push_back(it->start());}for (auto &it : _hander){it.join();}}
private:void ThreadWay(int i){cout << "thread %d run........" << i << endl;}vector<Thread*> _poll;vector<thread> _hander;
};
int main()
{ThreadPoll it;it.start(20);
}
``
关键字:discuz应用中心破解_动态网站制作素材_怎么去做网络推广_自助建站系统破解版

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: