c++ - 当我使用 long long int 和 int 作为参数时,为什么 max 函数会出现错误

标签 c++

我是 C++ 的新手,在使用 max 函数时遇到了这个意外错误,我在其中传递了 long long 和 int 类型的参数

当我将 (int,int) 作为参数或 (long long,long long) 传递时,它工作正常但与 (long long,int) 不兼容

ll painter(int board[],int n,int k)
{

 ll  s = 0,total_min;
 ll ans;

 for(ll i = 0;i < n;i++)
   { total_min += board[i];
       s = max(s,board[i]);

}

这是错误显示

prog.cpp:39:25: error: no matching function for call to 'max(int&, long long int&)
        s =max(s,board[i]);"

最佳答案

因为 std::max 是一个函数模板,其签名例如是

template< class T > 
const T& max( const T& a, const T& b );

完整列表位于:https://en.cppreference.com/w/cpp/algorithm/max

现在,在实例化模板时,您提供了两种不同的类型,但模板只需要一种。您必须明确指定模板参数,如本例所示:

#include <algorithm>

int main(){
    int a{5};
    long long int b{55};
    return std::max<long long int>(a, b);
}   

如评论中所述(用户 John),您还可以转换两者之一,例如,您可以将 int 转换为 long long

关于c++ - 当我使用 long long int 和 int 作为参数时,为什么 max 函数会出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57231493/

相关文章:

c++ - 编辑距离递归算法——Skiena

c++ - Boost 程序无法在 Linux 上运行

c++ - token 前的预期不合格 ID

c++ - 为什么我在 spoj 上获得 BUGLIFE 的 WA

c++ - CMake 与 Visual Studio 2017 C++ 核心指南检查器 (CppCoreCheck) 的集成

c++ - 我应该使用 lambda 还是仿函数作为比较函数?

c++ - 在 C++ 中索引整数指针

c++ - 将数字数组保存到特定文件中并稍后调用

c++ - 牛顿-拉夫森算法 : find all roots include negative

错误的 fprintf 的 C++ 等价物