为什么Max()在C+&#x2B中函数给出错误的“无匹配函数,可以呼叫最大匹配功能”?如果我在条件语句中明确执行同样的代码

发布于 2025-02-11 23:00:21 字数 629 浏览 1 评论 0原文

最大的两个参数均为 int 类型,那么为什么我会遇到此错误?

代码是在字符串

中找到最大的降亚深度。
int maxDepth(string s) {
        stack<char> stac;
        int maxDepth = 0;
        for(auto &elem: s) {
            if(elem == '(') 
                stac.push(elem);
            else if(elem == ')')
            {
                // maxDepth = max(maxDepth, stac.size()); // this doesn't work
                if(stac.size() > maxDepth) // this works, why?
                    maxDepth = stac.size();
                stac.pop();
            }
        }
        return maxDepth;
    }

Both parameters of max are of type int, then why am I getting this error?

Code is to find maximum depth of paranthesis in a string

int maxDepth(string s) {
        stack<char> stac;
        int maxDepth = 0;
        for(auto &elem: s) {
            if(elem == '(') 
                stac.push(elem);
            else if(elem == ')')
            {
                // maxDepth = max(maxDepth, stac.size()); // this doesn't work
                if(stac.size() > maxDepth) // this works, why?
                    maxDepth = stac.size();
                stac.pop();
            }
        }
        return maxDepth;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千寻… 2025-02-18 23:00:22

您的编译器拒绝您对std :: Max函数拒绝调用的原因是因为它无法推断出所需的类型。

的典型实现

 template<typename _Tp>
    _GLIBCXX14_CONSTEXPR
    inline const _Tp&
    max(const _Tp& __a, const _Tp& __b)
    {
      // concept requirements
      __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
      //return  __a < __b ? __b : __a;
      if (__a < __b)
    return __b;
      return __a;
    }

以下是std :: max _tp /language/template_argument_deduction“ rel =“ nofollow noreferrer”>模板类型扣除工作是评估每个模板参数的类型,然后比较它们是否匹配。由于您的一个参数之一是int,而另一个是size_t,它是一个无符号的int,因此您将有错误。

这是使用std :: Max

施放时可以做的事情,

  1. 因此参数匹配:

      maxDepth = max(static_cast&lt; size_t&gt;(maxDepth),stac.size()); 
     
  2. 声明类型,这也更可读:

      maxDepth = max&lt; size_t&gt;(maxDepth,stac.size())
     

关于您自己的程序,我建议您将maxDepth的类型更改为size> size_t

另外,对于可读性,请更改功能名称,以使其具有与变量相同的名称。

The reason why your compiler reject your call to the std::max function is because it cannot deduce the type it needs.

Below is a typical implementation of std::max

 template<typename _Tp>
    _GLIBCXX14_CONSTEXPR
    inline const _Tp&
    max(const _Tp& __a, const _Tp& __b)
    {
      // concept requirements
      __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
      //return  __a < __b ? __b : __a;
      if (__a < __b)
    return __b;
      return __a;
    }

Both parameters receive _Tp but the way that template type deduction works is it evaluates the type for each of the template parameters and then compares if they match. Since one of your parameters is an int and other is a size_t which is an unsigned int then you will have an error.

Here's what you can do when using std::max:

  1. Cast so the parameters match:

    maxDepth = max(static_cast<size_t>(maxDepth), stac.size()); 
    
  2. Declare the type, which is also more readable:

    maxDepth = max<size_t>(maxDepth, stac.size())
    

Regarding your own program, I advise that you change the type of maxDepth to size_t.

Also, for readability, change the function name so it does not have the same name as the variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文