please help me with max element function in c stl

0 votes

Please help me understand why this is providing an issue; I want to determine the maximum number of elements without utilising any extra space.

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});
Jun 27 in C++ by Nicholas
• 4,720 points
25 views

1 answer to this question.

0 votes

You can substitute max for *max element. 

Please see the following:

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

It will give your desired output.

answered Jun 27 by Damon
• 4,760 points

Related Questions In C++

0 votes
0 answers

Inbuilt __gcd(A,B) function in C++

I recently learned about a c++ specia ...READ MORE

Jul 11 in C++ by Nicholas
• 4,720 points
7 views
0 votes
0 answers

Accumulate function in Vector (STL) giving negative sum

I'm receiving a negative total when I run the code below (-294967296). #include<iostream> #include<vector> #include<numeric> using namespace std; int main() { ...READ MORE

Jul 22 in C++ by Nicholas
• 4,720 points
18 views
0 votes
0 answers

Storing C++ template function definitions in a .CPP file

I have some template code that I'd rather have saved in a CPP file rather than inline in the header.  I know this is possible if you know which template types will be utilised.  As an example: .h file class foo { public: template <typename ...READ MORE

Jul 26 in C++ by Nicholas
• 4,720 points
8 views
0 votes
1 answer

Is there any built-in factorial function in c++?

Although no C function is written particularly for computing factorials, the C math package allows you to compute the gamma function. Because (n) Equals (n-1)!  Using tgamma of i+1 on positive integers returns i!. for (int i = 1 ; ...READ MORE

answered 6 days ago in C++ by Damon
• 4,760 points
11 views
0 votes
1 answer

Syntax of priority queue

We must first include the queue header file in order to establish a priority queue in C++. #include <queue> Once we import this file, we ...READ MORE

answered May 31 in C++ by Damon
• 4,760 points
13 views
0 votes
0 answers

How to choose between map and unordered_map?

Assume I wanted to map data that ...READ MORE

Jul 4 in C++ by Nicholas
• 4,720 points
21 views
0 votes
1 answer

C++ `this` pointer

Pointer variables are used to store the ...READ MORE

answered May 31 in C++ by Damon
• 4,760 points
23 views
0 votes
1 answer

Why would anyone use set instead of unordered_set?

Unordered sets must compensate for their O(1) ...READ MORE

answered Jun 1 in C++ by Damon
• 4,760 points
51 views
0 votes
1 answer

Function default argument value depending on argument name in C++ [duplicate]

When the function is called with no argument for the corresponding parameter, the default argument is evaluated.  In a default argument, a parameter must not appear as a potentially evaluated expression.  A function's parameters declared before a default argument are in scope and can obscure the namespace and class member name. It provides the following example: int h(int a, ...READ MORE

answered Jun 7 in C++ by Damon
• 4,760 points
20 views
0 votes
1 answer

how could I use the power function in c/c++ without pow(), functions, or recursion

It is part of a series.  Replace pow() with the previous iteration's value. There is no need for code to call pow ().  Pow(x, 5 * I - 1) and pow(-1, I - 1) may be formed since both have an int exponent dependent on the iterator I from the previous loop iteration. Example: Let f(x, i) = pow(x, 5 * i ...READ MORE

answered Jun 21 in C++ by Damon
• 4,760 points
43 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP