大佬教程收集整理的这篇文章主要介绍了c – 搜索参数空间时避免使用嵌套的for循环,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
void tester_func(int p1,double p2,std::string const& p3);
和一些选定的参数
std::vector<int> vec_p1 = { 1,2,666 }; std::vector<double> vec_p2 = { 3.14159,0.0001 }; std::vector<std::string> vec_p3 = { "Method_smart","Method_Silly" };
我目前所做的很简单
for(auto const& p1 : vec_p1) for(auto const& p2 : vec_p2) for(auto const& p3 : vec_p3) tester_func(p1,p2,p3);
但是,Sean Parent建议避免使用显式循环并使用std :: algorithms.如何在上述案例中遵循这一建议?有成语吗?编写可执行模板的变量模板的最简洁方法是什么?没有C 11@L_262_8@的最佳方法是什么?
@H_404_4@
#include <tuple> #include <type_Traits> template <class TestFunction,class... Containers,class... Types> typename std::enable_if<sizeof...(Containers) == sizeof...(Types)>::type TestNextLevel ( TestFunction testFunction,const std::tuple<Containers...>& containerstuple,const Types&... parameters ) { testFunction(parameters...); } template <class TestFunction,class... Types> typename std::enable_if<(sizeof...(Containers) > sizeof...(Types))>::type TestNextLevel ( TestFunction testFunction,const Types&... parameters ) { for (const auto& element : std::get<sizeof...(Types)>(containerstuplE)) { TestNextLevel(testFunction,containerstuple,parameters...,element); } } template <class TestFunction,class... Containers> void TestAllCases ( TestFunction testFunction,const containers&... containers ) { TestNextLevel ( testFunction,std::tuple<const containers&...>(containers...) ); }
使用示例:
TestAllCases(tester_func,vec_p1,vec_p2,vec_p3);@H_404_4@ @H_404_4@
以上是大佬教程为你收集整理的c – 搜索参数空间时避免使用嵌套的for循环全部内容,希望文章能够帮你解决c – 搜索参数空间时避免使用嵌套的for循环所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。