Function that returns the max of a variable number of scalars

Oct 23, 2014 at 12:35pm
I need to create a function that takes as an input a variable number of scalars and returns the biggest one. Just like std::max() does for 2 elements, but I need it for an undefined number of elements, can be 2, or 5, or 10 etc.. Any suggestions on how to approach this?

What I need it for: I'm working with a bunch of vectors, maps, etc. and I need to find out which has the most elements. So I was thinking that I should end up with something like
int biggest = max(vector1.size(), vector2.size(), map1.size(), ...);
Last edited on Oct 23, 2014 at 12:48pm
Oct 23, 2014 at 12:46pm
You can use std::max on multiple values.
 
std::max({2, 8, 5, 3}) // returns 8 


If you want to find out the maximum number in a container you use std::max_element.
1
2
std::vector v{2, 8, 5, 3};
std::max_element(v.begin(), v.end()) // returns 8 


http://en.cppreference.com/w/cpp/algorithm/max
http://en.cppreference.com/w/cpp/algorithm/max_element
Oct 23, 2014 at 1:31pm
std::max_element(v.begin(), v.end()) // returns 8
max_element returns iterator. You need to dereference result to get value.
Oct 24, 2014 at 6:37am
None worked. I'm using VS2010

std::max({2, 8, 5, 3})
Error: no instance of overloaded function "std::max" matches the argument list

std::vector<int> v {2, 8, 5, 3};
Error: expected a ; (after v)

And more important, if I put this into a function, how do I have a variable number of inputs and how do I call them?
Last edited on Oct 24, 2014 at 6:45am
Oct 24, 2014 at 8:12am
solved

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int maxof(int nargs, ...)
{
    va_list ap;
    va_start(ap, nargs);
    
    int max = va_arg(ap, int);
    
    for(int i = 2; i <= nargs ; i++) 
    {
        int a = va_arg(ap, int);
        if(a > max) max = a;
    }
    
    va_end(ap);
    
    return max;
}
Oct 24, 2014 at 8:19am
int maxof(int nargs, ...)
Do not use ellipsis. It is dangerous and useless in your case. How would you read n values from input (where n is not known beforehand) and then find maximum?
Oct 28, 2014 at 10:41am
the n is known, i can find it. but it's variable
Oct 28, 2014 at 10:55am
but it's variable
And you cannot actually pass variable number of arguments to function. Number of arguments should be known ar compile time.

If you store elements in a vector you should use std::max_element:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <algorithm>
#include <iostream>
#include <vector>


int main()
{
	std::vector<int> v;
	//Solving lack of uniform initialization in VS2010
	v.push_back(4);
	v.push_back(2);
	v.push_back(12);
	v.push_back(6);
	// max_element returns iterator to found element
	//            ↓                so we need to dereference it
	int largest = * std::max_element(v.begin(), v.end());
	std::cout << largest;
}
12
Oct 28, 2014 at 2:12pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <type_traits>

template < typename A, typename B >
typename std::common_type<A,B>::type maxv( A&& a, B&& b ) { return a<b ? b : a ; }

template < typename FIRST, typename... REST >
typename std::common_type<FIRST,REST...>::type maxv( FIRST&& first, REST&&... rest )
{ return maxv( first, maxv( std::forward<REST>(rest)... ) ) ; }

int main()
{
    const std::string str = "mnopqr" ;
    const char cstr[] = "uvwxyz" ;
    
    std::cout << maxv( 233, 23.8, 1200LL, 675243LL, 'A', 123456, short(32) ) << '\n'
              << maxv( str, cstr, "abcdef", std::string( 5, '!' ) ) << '\n' ;
              
    static_assert( std::is_same< std::string, decltype( maxv( str, cstr ) ) >::value , "unexpected" ) ;
}

http://coliru.stacked-crooked.com/a/0555fc26cf48cf33
Topic archived. No new replies allowed.