pb通过对象名称调用对象_C++ 可调用对象(一)

点击上方“蓝字”,发现更多精彩。基本概念①.调用运算符即:() 。跟随在函数名之后的一对括号 “()”,起到调用函数的效果,传递给函数的实参放置在括号内。②.谓词是一个可调用表达式,其返回结果是一个能用作条件的值。根据可接收的参数分为一元谓词和二元谓词;接收谓词参数的算法对输入序列中的每个元素调用谓词。③.可调用对象对于一个对象或者一个表达式,如果可以对其使用调用运算符,则称为可调用对象。④.调用形式一种调用形式对应了一个函数类型,指明了调用返回类型以及传递的参数类型,比如:int (int,int) 。函数对象01PART定义

①.概念

如果类定义了调用运算符,则该类的对象称作函数对象,即这些对象的行为像函数一样。

②.定义

函数对象除了 operator() 之外也可以包含其他成员。

class PrintString {public:    PrintString(ostream &o = cout,char c = ''):os(o),sep(c) {}    void operator()(const string &s) const//重载函数调用运算符    {        os << s << sep;    }private:    ostream &os;    char sep;}PrintString printer;printer("hello world");//类似函数调用
02PART标准库中可调用对象

①.标准库函数对象

标准库在头文件 functional 中定义了一系列模版类,每个类都分别定义了一个执行命名操作的调用运算符,如:less 、greater 等。

②.使用

表示运算符的函数对象常用来替换算法中的默认运算符,比如排序算法默认使用 operator< 将序列按升序排列,我们可以传入一个 greater 对象,使按降序排列。

vector<string> vec {"111","2222","33"};sortstring>()>;
03PART可调用对象与 function

①.不同类型可具有相同调用形式

//普通函数int add(int i,int j) { return i+ j }//函数指针int (*addptr)(int i,int j) = add;//lanbda 表达式auto mod = [](int i,int j) {return i%j }//函数对象类class divide{    int operator()(int i,int j)    {        return i/j;    }}//以上各种类型的可调用对象都具有相同的调用形式 int(int,int)

②.标准库 function 类型

function 定义在头文件 functional 中,是一个模版类型,可以用来存储可调用对象。

//普通函数int add(int i,int j) { return i+ j }//函数指针int (*addptr)(int i,int j) = add;//lanbda 表达式auto mod = [](int i,int j) {return i%j }//函数对象类class divide{    int operator()(int i,int j)    {        return i/j;    }}function<int(int,int)> f1 = add;function<int(int,int)> f2 = divide();//函数对象类function<int(int,int)> f3 = [](int i,int j) {return i%j };function<int(int,int)> f4 = addptr;//函数指针
bind 函数01PART定义①.概念可将 bind 函数看作是一个通用的函数适配器,它接受一个可调用对象,生成一个新的可调用对象来适应源对象的参数列表。调用 bind 的一般形式:auto newCallable = bind(callable,arg_list);当我们调用 newCallable 时,newCallable 会调用 callable ,并传给它 arg_list 中的参数。②.占位符arg_list 中的参数可能包含形如 _n 的名字,这些参数是“占位符”,表示 newCallable 的参数。数值 n表示生成的可调用对象中参数的位置:_1 为 newCallable的第一个参数,_2 为第二个参数,以此类推。③.头文件标准库函数 bind 定义在头文件 functional 中,并且使用时需要 using namespace std::placeholders。02PARTbind 使用

①.参数绑定

通过参数绑定可以更改可调用对象的实际需要外部传递的参数数量。

6d08211bd7f098d963e619cfeb984a68.png

②.重排参数顺序

通过 bind 可以重排传递的参数顺序。

fa48b59cd1e63d91aaf28fa15302715c.png

③.绑定引用参数

 bind 中绑定引用参数需要使用标准库函数 ref ,不能使用 &。

ostream &os = cout;ostream &print(ostream &os,const string &s,char c){    return os << s << c;}// 上述函数中 ostream 对象必须传递引用,用 bind 绑定如下auto p = bind(print,ref(os),_1,c);
8b64a5cfb91202fbea59b7d66ec27d9c.png扫码关注持续获取最新文章c++学习 算法与数据结构
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++调用.pb文件可以使用TensorFlow C++ API。下面是一个简单的例子,展示了如何使用TensorFlow C++ API加载.pb文件并进行推理: ```c++ #include <fstream> #include <iostream> #include "tensorflow/cc/saved_model/loader.h" #include "tensorflow/core/framework/tensor.h" int main() { // 加载模型 tensorflow::SavedModelBundle bundle; tensorflow::SessionOptions session_options; tensorflow::RunOptions run_options; tensorflow::Status status = tensorflow::LoadSavedModel( session_options, run_options, "/path/to/model", {"serve"}, &bundle); if (!status.ok()) { std::cerr << "Error loading model: " << status.ToString() << std::endl; return 1; } // 构造输入张量 tensorflow::Tensor input_tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape({1, 224, 224, 3})); auto input_tensor_mapped = input_tensor.tensor<float, 4>(); // TODO: 填充输入张量的值 // 进行推理 std::vector<tensorflow::Tensor> outputs; status = bundle.GetSession()->Run({{"input", input_tensor}}, {"output"}, {}, &outputs); if (!status.ok()) { std::cerr << "Error running inference: " << status.ToString() << std::endl; return 1; } // 处理输出张量 auto output_tensor_mapped = outputs[0].tensor<float, 2>(); // TODO: 处理输出张量的值 return 0; } ``` 上述代码中,首先使用`tensorflow::LoadSavedModel`函数加载.pb文件。然后,构造输入张量,并将其传递给`bundle.GetSession()->Run`函数进行推理。最后,处理输出张量的值。 需要注意的是,推理时需要指定输入张量的名称和输出张量的名称。在上述代码中,输入张量的名称为`"input"`,输出张量的名称为`"output"`。这些名称需要与.pb文件中的节点名称相对应。 另外,在编译时需要链接TensorFlow的静态库,具体方法可以参考TensorFlow C++ API的文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值