2016-07-27 178 views
-3

我有一個向量textCommands它保存結構稱爲TextCommand包含RECT和一個字符串;並且RECT具有全部在屏幕座標中的值topleftbottomright。我想知道如何分類這個向量,然後我可以調用std::unique並刪除重複條目。重複條目是具有相同字符串的條目,以及相同的所有值都相同的RECT排序屏幕座標

//Location in screen coordinates(pixels) 
struct RECT 
{ 
    int top; 
    int left; 
    int bottom; 
    int right; 
}; 

//text at location RECT 
struct TextCommand 
{ 
    std::string text; 
    RECT pos; 
}; 

std::vector<TextCommand> textCommands; 
+1

你說什麼?使用'std :: sort'。 –

+0

@ CaptainObvious你打敗了我。 http://en.cppreference.com/w/cpp/algorithm/sort – Ceros

+0

@CaptainObvlious我可以排序哪個參數? – Bcmonks

回答

0

你需要一個自定義的比較(仿函數,λ,或超載operator <)將滿足嚴格的弱序,你可以送入std::sortstd::set。最簡單的一個是:

#include <tuple> 

struct TextCommandCompare 
{ 
    bool operator()(TextCommand const& a, TextCommand const& b) const 
    { 
     return std::tie(a.text, a.pos.top, a.pos.left, a.pos.bottom, a.pos.right) < 
      std::tie(b.text, b.pos.top, b.pos.left, b.pos.bottom, b.pos.right); 
    } 
}; 

std::tie創建std::tuple實現你的字典比較。