C++ How to Sort Vector making use of Template
Hi Guys, thanks for looking at my question.
I got a Templates.h file that goes this way..
/* Template Less Than */
template<typename T>
bool lessThan(T a,T b)
{
return a<b;
}
/* Template greater Than */
template<typename T>
bool greaterThan(T a,T b)
{
return a>b;
}
/* Template Equals */
template<typename T>
bool equals(T a,T b)
{
return a==b;
}
Then i got this class
Map2D
About Map2D..
class Map2D
{
protected:
int x;
int y;
public:
Map2D();
Map2D(int,int);
int getX();
int getY();
};
At my main.cpp i got a vector class of Map2D
vector<Map2D> map2d;
So now i need to sort it by X Ascending.. how do i make use of the template file to do a sort on the vector of it X Ascending.. Consider i will need overload another for DESCENDING later..
Normally i will use
sort(map2d.begin(),map2d.end(),sortByX);
and sortByX will be a struct with overload by it () operator.
But the question now is since i got a template that is lesser than and greater than.. how can i make use of it to sort X by ascending and then another X by descending with the template generic function of Templates.H .
Updates:
I think i need to overload the class Map2D operator > , < and ==
but my question is how do i overload it with the help of MyTemplates.h function such as lesserThan , greaterThan, equals
Thanks.