feelfree
feelfree

Reputation: 11753

How to set self-defined default compare function in STL

Suppose now I have an array of coordinates in the 2D dimension, and I want to select two coordinates based on two criteria:

In order to fulfilling this task, I have defined the following following functions:

template <typename T>
class  Coordinate //:public common::BasicCoordinate<T>
{
public:
    T x_;  ///< x_coordinate
    T y_;  ///< y_coordinate
};


template<typename T>
struct compare_x_coordinate 
{
    bool operator() (const Coordinate<T> &i,const Coordinate<T> &j) 
    { return i.x_<j.x_; }
} ;

template<typename T>
struct compare_y_coordinate 
{
    bool operator() (const Coordinate<T> &i,const Coordinate<T> &j) 
    { return i.y_<j.y_; }
} ;

Then what I am going to do is to write a function that select two coordinates from a range of coordinates based on either compare_x_coordinate or compare_y_coordinate. I can do this with two functions:

template<typename T >
    void find_left_right_points(const std::vector<Coordinate<T> > &ptArray, 
        Coordinate<T> &left, 
        Coordinate<T> &right )
    {
       compare_x_coordinate<T> mycompare; 
        std::vector<Coordinate<T> >::const_iterator it_max = std::max_element(ptArray.begin(), ptArray.end(), mycompare);
        int index_max = it_max-ptArray.begin();


        std::vector<Coordinate<T> >::const_iterator it_min = std::min_element(ptArray.begin(),ptArray.end(),mycompare); 
        int index_min = it_min-ptArray.begin();

        left    = ptArray[index_min];
        right   = ptArray[index_max];
    } ;

and

template<typename T >
void find_top_bottom_points(const std::vector<Coordinate<T> > &ptArray, 
    Coordinate<T> &left, 
    Coordinate<T> &right )
{
   compare_y_coordinate<T> mycompare; 
    std::vector<Coordinate<T> >::const_iterator it_max = std::max_element(ptArray.begin(), ptArray.end(), mycompare);
    int index_max = it_max-ptArray.begin();


std::vector<Coordinate<T> >::const_iterator it_min = std::min_element(ptArray.begin(),ptArray.end(),mycompare); 
int index_min = it_min-ptArray.begin();

left    = ptArray[index_min];
right   = ptArray[index_max];
} ;

Of course, the best one is to combine these two functions into one:

template<typename T >
    void find_points(const std::vector<Coordinate<T> > &ptArray, 
        Coordinate<T> &left, 
        Coordinate<T> &right, 
        // I do not know how to write the default comparasion function 
        )
    {
     //  compare_x_coordinate<T> mycompare; 
        std::vector<Coordinate<T> >::const_iterator it_max = std::max_element(ptArray.begin(), ptArray.end(), mycompare);
        int index_max = it_max-ptArray.begin();


        std::vector<Coordinate<T> >::const_iterator it_min = std::min_element(ptArray.begin(),ptArray.end(),mycompare); 
        int index_min = it_min-ptArray.begin();

        left    = ptArray[index_min];
        right   = ptArray[index_max];
    } ;

However, I do not know how to write the default comparison function in the above example, any ideas? Thanks.

EDIT: A possible application of the function should be:

void main(void)
{
  std::vector<Coordinate> ptArray;
  // step 1: fill the coordinates 
  ptArray.push_back(...)
  // step 2: select the most left and right coordinates
  Coordinate left, right;
  find_points(ptArray,left,right);
  // step 3: select the top and bottom coordinates
  Coordinate top,bottom;
  find_points(ptArray, top,left, find_top_bottom_points);

}

Upvotes: 2

Views: 152

Answers (1)

Anycorn
Anycorn

Reputation: 51475

If I understood correctly, one way to do it is:

template<typename T, class Compare >
void find_points(const std::vector<Coordinate<T> > &ptArray, 
                 Coordinate<T> &left, 
                 Coordinate<T> &right,
                 const Compare &cmp) {
    find_points(ptArray, left, right, cmp);
}

template<typename T >
void find_points(const std::vector<Coordinate<T> > &ptArray, 
                 Coordinate<T> &left, 
                 Coordinate<T> &right) {
    find_points(ptArray, left, right, default_compare);
}

you can also do it using boost::function (but you may forfeit performance):

template<typename T >
void find_points(const std::vector<Coordinate<T> > &ptArray, 
                 Coordinate<T> &left, 
                 Coordinate<T> &right,
                 const boost::function<bool(const Coordinate<T>&, Coordinate<T>&)> &cmp =
                     compare_x_coordinate<T>())
{
    ...
}

Upvotes: 1

Related Questions