Suppose I have these two functions:

template<class T>
bool largerthan(T a, T b)
{
	if(a>b)
		return true;
	return false;
}

template<class T>
int reduce(T ar[], int n)
{
	list<T> buffer;
	buffer.insert(buffer.begin(), ar, ar+n);
	buffer.sort(largerthan);
	buffer.unique();
	copy(buffer.begin(),buffer.end(), ar);
	return buffer.size();
}

I have two kinds of arrays for reduce(): string and long;
something happen to the red part(buffer.sort(largerthan)), the complier says that it couldn't using the function for buffer.sort(), I am not sure how to write it, and if the complier was able to pass the using type to largerthan().

Waiting for your answer, thanks.^-^

Edited 6 Years Ago by Nick Evan: Added CODE-tags.

Hello dspjm,

1. In the reduce() function, add the template parameter for the largerthan() function.

2. That is :

buffer.sort(largerthan<T>);


- Bio.

We don't need to write largerthan<>() when std::greater<>() in <functional> is available.

We can also perform the reduce() operation directly on the array; without using a temporary list<>

#include <algorithm>
#include <functional>

template< typename T >
std::size_t reduce( T ar[], std::size_t n )
{
    std::sort( ar, ar+n, std::greater<T>() ) ;
    return std::unique( ar, ar+n ) - ar ;
}
This question has already been answered. Start a new discussion instead.