Max Function

I am trying to determine the maximum of 2 numbers. I have a cpp file with the following code (my actual cpp is significantly more complex than this, but this is the bare bones of the problem):

1
2
3
4
5
6
7
#include "class1.h"
#include <algorithm>

void class1::function1()
{
     a = max(1.0, 2.0);
}


The compiler keeps throwing an error, saying that the algorithm header is antiquated, and to use one of the 32 headers found in 17.4.1.2 of the C++ standard. I thought that algorithm was still valid. Is it not valid anymore? (I'm using Eclipse Indigo Service Release 1, if that matters.) I know that I could just write a max function myself, but I was really hoping to use the built-in max function itself. Thanks!
couldn't you just use an if statement?

if(1.0>2.0)
{
//do stuff
}
else
//do other stuff
Last edited on
<algorithm> is not remotely deprecated in C++. Do you know which compiler you are using?
Last edited on
Yes, I could (I already mentioned this), but I was hoping to use the built-in max function.
How do I determine what my compiler is? I'm on a Mac 10.5.8, if that matters.
Run it from a command line with -v or --version usually does it.

For example, I'm using g++ to compile, and I would type
g++ --version

This does rely on you knowing what the command to run the compiler is.
I don't really know how to use terminal. I use the Eclipse GUI strictly. I'm sure I can look up how to do this, though.
I don't really know how to use terminal.


You click the button marked "terminal" or some such, and you get a box that looks mostly empty with a blinking line in it. Then, you type. I would type

g++ --version and then press Enter.

You might as well try the same thing.
Last edited on
This is what I got from terminal:

i686-apple-darwin9-g++-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5488)
V4.0.0 is about 6 years old, so presumably 4.0.1 could probably do with updating but it shouldn't have any problems with the <algorithm> header or library.

Here is 17.4.1.2 of the C++ 2003 standard:

17.4.1.2 Headers

The elements of the C + + Standard Library are declared or defined (as appropriate) in a header.

The C + + Standard Library provides 33 C + + headers, as shown in Table 11:

Table 11—C++ Library Headers
_____________________________________________________________
<algorithm> <iomanip> <list> <queue> <streambuf>
<bitset> <ios> <locale> <set> <string>
<complex> <iosfwd> <map> <sstream> <typeinfo>
<deque> <iostream> <memory> <stack> <utility>
<exception> <istream> <new> <stdexcept> <valarray>
<fstream> <iterator> <numeric> <strstream> <vector>
<functional> <limits> <ostream> _
That is strange that 4.0.1 isn't recognizing the <algorithm> header, then. Perhaps I should just write my own max function.
Try this




#include <cstdlib>
#include <iostream>

using namespace std;

int max(int a, int b)
{
int x = a > b ? a:b;
return (x);
}


int main(int argc, char *argv[])
{
int RES = max(10, 20);
cout << RES << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Topic archived. No new replies allowed.