2014-03-27 70 views
2

現在假設我有在2D維座標的數組,我想選擇兩個座標基於兩個標準:如何設置自定義的默認比較函數在STL

  • 選擇最左邊和最右邊的座標
  • 選擇頂部和底部座標

爲了完成這個任務,我已經定義了以下功能如下:

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_; } 
} ; 

然後我要做的是編寫一個函數,根據compare_x_coordinatecompare_y_coordinate從座標範圍中選擇兩個座標。 我可以用兩個功能做到這一點:

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]; 
    } ; 

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]; 
} ; 

當然,最好的就是這兩個功能結合成一個:但是

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]; 
    } ; 

,我做不知道在上面的例子中如何編寫默認比較函數,有什麼想法?謝謝。

編輯:函數的一種可能的應用應該是:

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); 

} 
+0

你在找4個座標? (leftmost.x,leftmost.y),(rightmost.x,rightmost.y),(topmost.x,topmost.y),(bottommost.x,bottommost.y)??或者你正在尋找2個座標?或4個不同的值 – Ravi

+0

@Ravi,感謝您的評論。我已經舉了一個例子來展示如何使用這個函數。 – feelfree

+0

從幾何角度來看,你應該確定'compare_xy_coordinate'和'compare_yx_coordinate'。所以如果你有兩個相同的'x'座標,你總是得到相同的座標,結果不取決於容器的順序。 – pmr

回答

1

如果我理解正確的,這樣做的一個方法是:

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); 
} 

你也可以做使用boost ::它功能(但你可以放棄性能):

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>()) 
{ 
    ... 
}