Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 通行方式>;1 C+中的向量+;lambda函数_C++_Lambda - Fatal编程技术网

C++ 通行方式>;1 C+中的向量+;lambda函数

C++ 通行方式>;1 C+中的向量+;lambda函数,c++,lambda,C++,Lambda,我想从两个向量s和v计算s[I]*v[I]的最大值 我想知道是否有一种方法可以利用类似形式的lambda函数: min(s.begin()、s.end()、[](){})其中还可以包括v.begin()和v.end()?尝试以下方法: #include <limits> #include <numeric> #include <algorithm> #include <functional> int max = std::inner_product

我想从两个向量
s
v
计算
s[I]*v[I]
的最大值

我想知道是否有一种方法可以利用类似形式的lambda函数:

min(s.begin()、s.end()、[](){})
其中还可以包括
v.begin()
v.end()

尝试以下方法:

#include <limits>
#include <numeric>
#include <algorithm>
#include <functional>
int max = std::inner_product(s.begin(), s.end(), v.begin(), std::numeric_limits<int>::min(), 
        static_cast<int const&(*)(int const&, int const&)>(std::max), std::multiplies<int>());
#包括
#包括
#包括
#包括
int max=std::内部乘积(s.begin()、s.end()、v.begin()、std::numeric_limits::min(),
静态_cast(std::max),std::multiplies();
当然,假设
s
v
int
的向量,并且大小相同


EDIT我删除了lambdad
[](const int&a,const int&b){return std::max(a,b);}

没有简单的标准库函数来执行您想要执行的操作

说真的,有什么比下面更简单、更容易理解的呢

int maxProduct = std::numeric_limits<int>::min();
for ( size_t it = 0; it != s.size(); ++it )
{
   maxProduct = std::max(maxProduct, s[it]*v[it]);
}
int maxProduct=std::numeric_limits::min();
对于(size_t it=0;it!=s.size();+it)
{
maxProduct=std::max(maxProduct,s[it]*v[it]);
}

到目前为止,这是正确的答案
Internal_产品
旨在解决OP的问题!介绍如何按照您的意愿传递
std::max
,这很难看,因为您必须消除重载:
static\u cast(std::max)
。使用lambda更干净,这正是答案中推荐的。您可以通过值将参数传递给lambda,使其更简洁。任何一个健全的编译器都会内联所有的东西,这样无论是
std::max
还是lambda都不会作为单独的实体存在。你也可以为
std::multiples添加
:)当这起作用的时候,我认为这是对内积概念的滥用。这是我能想到的唯一方法,那就是表达式(与声明相反)这正是我认为OP想要的。@Holt但有:)