2016-07-27 147 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实现你的字典比较。