请帮助我使用 c++ stl 中的 max_element 函数
please help me with max_element function in c++ stl
下面是我的代码,我是 C++ 的新手,请帮助解释为什么会出现错误,我想在元素 中找到最大值而不使用任何额外的 space。
代码:
#include <bits/stdc++.h>
using namespace std;
int main() {
cout<<*max_element({4,6,2,5});
}
错误:
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
使用迭代器遍历列表以找到最大元素(参考:cppreference)。因此,一种方法是首先将值分配给向量,然后将其开始和结束迭代器传递给函数。
#include <vector>
...
int main() {
std::vector<int> nums = {4,6,2,5};
cout << *max_element(nums.begin(), nums.end());
}
您可以选择使用 std::max
来获取最大元素。它有一个重载来接受初始化列表,并使您能够按原样完成它(参考:cppreference)。
int main() {
cout << max({4,6,2,5});
}
注意:尽量不要使用 #include <bits/stdc++.h
,如本帖所述:
您可以使用最大值代替 *max_element。参考如下:
cout<<max({4,6,2,5});
它会给出你想要的输出。
下面是我的代码,我是 C++ 的新手,请帮助解释为什么会出现错误,我想在元素 中找到最大值而不使用任何额外的 space。
代码:
#include <bits/stdc++.h>
using namespace std;
int main() {
cout<<*max_element({4,6,2,5});
}
错误:
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
使用迭代器遍历列表以找到最大元素(参考:cppreference)。因此,一种方法是首先将值分配给向量,然后将其开始和结束迭代器传递给函数。
#include <vector>
...
int main() {
std::vector<int> nums = {4,6,2,5};
cout << *max_element(nums.begin(), nums.end());
}
您可以选择使用 std::max
来获取最大元素。它有一个重载来接受初始化列表,并使您能够按原样完成它(参考:cppreference)。
int main() {
cout << max({4,6,2,5});
}
注意:尽量不要使用 #include <bits/stdc++.h
,如本帖所述:
您可以使用最大值代替 *max_element。参考如下:
cout<<max({4,6,2,5});
它会给出你想要的输出。