Analyzing the largest number among many.....

As what the title said.....

Is there any function to do this ?
You can use std::max_element to find the largest value stored in a container. Not sure that is what you want.

http://en.cppreference.com/w/cpp/algorithm/max_element
Last edited on
Also in C++11 you can do this, using the initialiser list version of the std::max() function:
1
2
3
4
5
6
7
8
#include <algorithm>    // std::max

    int a = 5;
    int b = 12;
    int c = 17;
    int d = 13;
    int e = 9;  
    cout << "max({a, b, c, d, e}) == " << std::max({a, b, c, d, e}) << '\n';


max({a, b, c, d, e}) == 17

http://www.cplusplus.com/reference/algorithm/max/
Last edited on
Chervil wrote:
Also in C++11 you can do this, using the initialiser list version of the std::max() function:

You could to it in C++03 too, using the binary version of std::max() multiple times.

1
2
3
4
5
6
7
8
#include <algorithm>    // std::max

    int a = 5;
    int b = 12;
    int c = 17;
    int d = 13;
    int e = 9;  
    cout << "max(a, b, c, d, e) == " << std::max(a, std::max(b, std::max(c, std::max(d, e)))) << '\n';
Last edited on
Topic archived. No new replies allowed.