简体   繁体   English

请帮助我处理 c++ stl 中的 max_element function

[英]please help me with max_element function in c++ stl

Below is my code, I am new to C++ please help why this is giving error, I want to find max among element without using any extra space .下面是我的代码,我是 C++ 的新手,请帮助解释为什么这会出错,我想在不使用任何额外空间的情况下在元素中找到最大值。

Code:代码:

#include <bits/stdc++.h>
using namespace std;
    
int main() {
    cout<<*max_element({4,6,2,5});
}

Error:错误:

    error : prog.cpp: In function ‘int main()’:
    prog.cpp:5:30: error: no matching function for call to ‘max_element(<brace-enclosed initializer list>)’
      cout<<*max_element({4,6,2,5});
                                  ^
    In file included from /usr/include/c++/5/algorithm:62:0,
                     from /usr/include/x86_64-linux-gnu/c++/5/bits/stdc++.h:64,
                     from prog.cpp:1:
    /usr/include/c++/5/bits/stl_algo.h:5505:5: note: candidate: template<class _FIter> constexpr _FIter std::max_element(_FIter, _FIter)
         max_element(_ForwardIterator __first, _ForwardIterator __last)
         ^
    /usr/include/c++/5/bits/stl_algo.h:5505:5: note:   template argument deduction/substitution failed:
    prog.cpp:5:30: note:   candidate expects 2 arguments, 1 provided
      cout<<*max_element({4,6,2,5});
                                  ^
    In file included from /usr/include/c++/5/algorithm:62:0,
                     from /usr/include/x86_64-linux-gnu/c++/5/bits/stdc++.h:64,
                     from prog.cpp:1:
    /usr/include/c++/5/bits/stl_algo.h:5529:5: note: candidate: template<class _FIter, class _Compare> constexpr _FIter std::max_element(_FIter, _FIter, _Compare)
         max_element(_ForwardIterator __first, _ForwardIterator __last,
         ^
    /usr/include/c++/5/bits/stl_algo.h:5529:5: note:   template argument deduction/substitution failed:
    prog.cpp:5:30: note:   candidate expects 3 arguments, 1 provided
      cout<<*max_element({4,6,2,5});
                                 ^

std::max_element uses iterators to iterate through a list to find the maximum element (reference: cppreference ). std::max_element使用迭代器遍历列表以找到最大元素(参考: cppreference )。 So one way of doing it is by first assigning the values to a vector and then passing its begin and end iterators to the function.因此,一种方法是首先将值分配给向量,然后将其开始和结束迭代器传递给 function。

#include <vector>
...

int main() {
    std::vector<int> nums = {4,6,2,5};
    cout << *max_element(nums.begin(), nums.end());
}

Optionally you can use std::max to get the max element.或者,您可以使用std::max来获取最大元素。 It has an overload to take in a initializer list and enables you to get it done as is (reference: cppreference ).它有一个重载来接收初始化列表,并使您能够按原样完成它(参考: cppreference )。

int main() {
    cout << max({4,6,2,5});
}

Note: Try not to use #include <bits/stdc++.h as explained in this thread: Why should I not #include <bits/stdc++.h>?注意:尽量不要使用#include <bits/stdc++.h ,如该线程中所述: 为什么我不应该#include <bits/stdc++.h>?

you can use max instead of *max_element.您可以使用 max 代替 *max_element。 Refer below:参考以下:

cout<<max({4,6,2,5});

It will give your desired output.它将提供您想要的 output。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM