第5篇 c++ 函数的多返回值实现-返回多个值
c++ 函数的多返回值实现
std::tuple<Mat, int, double, std::string> AuatoPafackSydstem::GetMatchingValue(Mat mat_img, std::string img_template_path)
{
Mat a;
return {a,1,0.001,""};
}
std::tuple<Mat, int, double, std::string> GetMatchingValue(Mat mat_img, std::string img_template_path)
{
Mat a;
return {a,1,0.001,""};
}
Mat aa1;
//C++14
std::tuple<Mat,int,double,std::string> tuple1 = GetMatchingValue(aa1,"");
aa1 = std::get<0>(tuple1);
int a2 = std::get<1>(tuple1);
double a3 = std::get<2>(tuple1);
std::string a4 = std::get<3>(tuple1);
auto a5 = 666;
//C++17支持
auto [a,b,c,d] = GetMatchingValue(aa1, "");
结构体:
// 定义一个结构体
struct BoxResult {int ret1;int mod1;
};// 函数返回 BoxResult
BoxResult box_mod(int a, int b) {return BoxResult {a / b, a % b};
}int main() {auto result = box_mod(15, 5);std::cout << "ret1 = " << result.ret1<< ", mod1 = " << result.mod1<< "\n";
}
Pair方法:
#include <utility>std::pair<int,int> box_mod(int a, int b) {return {a / b, a % b};
}int main() {auto [tt, xx] = box_mod(15, 5); // C++17的结构化绑定std::cout << "shang = " << tt << ", yushu = " << xx << "\n";
}