2015-05-23 21 views
0

我能夠分別讀取文件和填充矢量。創建泛型模板類,通過變量對不同類進行排序

有沒有一種方法可以創建一個模板來按照一個標準對各個類進行排序? 我使用的方法是單調乏味的,我必須創建很多模板類來適應每個向量。

我需要一些想法上創建一個通用模板

//consist if accessors and mutator x and y 
vector<Point2D> point2d; 
//consist if accessors and mutator x and y and z 
vector<Point3D> point3d; 
//consist if accessory and mutator 2 Point2D point 
vector<Line2D> line2d; 
//consist if accessory and mutator 2 Point3D point 
vector<Line3D> line3d; 
//sort_crit is a global variable that change according to user menu 
//sort_order is a global variable that change according to user menu (ASC or DSC) 
if(sort_crit == "X-Coordinate") { 
    sortByX(point2d, point2d.size(), sort_order);   
} 

我的數據

Point2D, [3, 2] 
Line3D, [7, 12, 3], [-9, 13, 68] 
Point3D, [1, 3, 8] 
Line3D, [7, -12, 3], [9, 13, 68] 
Point3D, [6, 9, 5] 
Point2D, [4, 8] 
Line3D, [70, -120, -3], [-29, 1, 268] 
Line2D, [7, 12], [-9, 4] 
Line3D, [25, -69, -33], [-2, -41, 58] 
Point3D, [6, 9, -50] 
Point2D, [12, 80] 
Point2D, [9, 8] 

我現在的模板只爲的Point2D

template <class T> 
void sortByX(vector<T> a1, int size, string type) { 
if (type == "ASC") { 

    for(int x=0; x<size; x++) { 

     for(int y=0; y<size-1; y++) { 

      if(a1[y].getX()>a1[y+1].getX()) { 

       int tempx = a1[y+1].getX(); 

       a1[y+1].setX(a1[y].getX()); 

       a1[y].setX(tempx); 

       int tempy = a1[y+1].getY(); 

       a1[y+1].setY(a1[y].getY()); 

       a1[y].setY(tempy); 
      } 
     } 
    } 
} else if (type == "DSC") { 

    for(int x=0; x<size; x++) { 

      for(int y=0; y<size-1; y++) { 

       if(a1[y].getX()<a1[y+1].getX()) { 

        int tempx = a1[y+1].getX(); 

        a1[y+1].setX(a1[y].getX()); 

        a1[y].setX(tempx); 

        int tempy = a1[y+1].getY(); 

        a1[y+1].setY(a1[y].getY()); 

        a1[y].setY(tempy); 
       } 
      } 
     } 
} 
    for(int x=0; x<size; x++) { 
     cout << a1[x] << endl; 
    } 

} 
+0

目前還不清楚您是否有多個不同的排序標準。 – b4hand

+0

沒有你向我們展示你的各種類的接口,也不可能建議一個通用的實現。 – b4hand

回答

0

工作更好的方式來做到這一點爲每個相應的排序順序創建單獨的比較器類,這些排序順序一般適用於所有的排序順序spective類型可以傳遞給內置的std::sort算法。

template<typename T> 
class ByXAscending { 
    bool operator()(T const &lhs, T const &rhs) const { 
     return (lhs.getX() < rhs.getX() 
       || (lhs.getX() == rhs.getX() 
        && lhs.getY() == rhs.getY())); 
    } 
} 
0

std::sort是通用排序,允許您提供自定義排序標準。

例如,對點,線一些容器等,l

sort(begin(l), end(l), [](auto& a, auto& b) 
{ 
    return a.getX() < b.getX(); 
}); 
+1

注意:通用lambdas是一個C++ 14功能。 –

0

爲什麼推出自己的排序功能?嘗試這樣的代替:

#include <iostream> 
#include <vector> 
#include <algorithm> 

using namespace std; 

struct Point2D 
{ 
    Point2D(int x=0, int y=0) : x(x), y(y) {} 
    int x; 
    int y; 
}; 

ostream& operator<<(ostream &os, const Point2D &p) 
{ 
    os << "{" << p.x << ", " << p.y << "}"; 
    return os; 
} 

int main(int argc, char *argv[]) 
{ 
    vector<Point2D> points; 
    points.push_back(Point2D(3, 3)); 
    points.push_back(Point2D(27, 5)); 
    points.push_back(Point2D(7, 4)); 
    points.push_back(Point2D(13, 26)); 
    points.push_back(Point2D(9, 5)); 


    cout << "Before sort:" << endl; 
    for (const auto p : points) 
     cout << p << " "; 
    cout << endl; 

    sort(points.begin(), points.end(), [](const Point2D &l, const Point2D &r) 
     { 
      return (l.x<r.x); 
     }); 

    cout << "Sort by x:" << endl; 
    for (const auto p : points) 
     cout << p << " "; 
    cout << endl; 

    sort(points.begin(), points.end(), [](const Point2D &l, const Point2D &r) 
     { 
      return (l.y<r.y); 
     }); 

    cout << "Sort by y:" << endl; 
    for (const auto p : points) 
     cout << p << " "; 
    cout << endl; 

    return 0; 
}