【发布时间】:2021-05-19 11:34:42
【问题描述】:
下面是我的代码,我是 C++ 新手,请帮忙解释一下为什么会出错,我想在元素中找到最大值 而不使用任何额外的空间。
代码:
#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::cout << std::max({4,6,2,5});.
标签: c++ data-structures stl c++14