#include <algorithm>
#include <iostream>
#include <vector>
template <typename T>
class ascOrder
{
public:
bool operator()(const T& a, const T& b)
{
return (std::fabs(a) < std::fabs(b));
}
};
template <typename T>
class revOrder
{
public:
bool operator()(const T& a, const T& b)
{
return (std::fabs(a) > std::fabs(b));
}
};
int main()
{
std::vector<int> nums = {3, 4, 2, 5, 1}; // Pre-defined random order
for(int i : nums)
{
std::cout << i << std::endl;
}
std::sort(nums.begin(), nums.end(), revOrder<int>());
std::cout << "Reverse Order" << std::endl;
for(int i : nums)
{
std::cout << i << std::endl;
}
return 0;
}
Just a little snippet showing how to use functors along with std::sort.
KYLE EDWARDS
Monday, 2 September 2013
Sort With Functors
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment