2016-07-27 19 views
-3

私はという文字列を含むTextCommandという構造体を保持するVectorを持っています。 RECTは、値がtop,left,bottom、およびrightであり、すべてスクリーン座標で表示されます。私は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

@Captainあなたは私にそれを打つことができました。 http://ja.cppreference.com/w/cpp/algorithm/sort – Ceros

+0

@CaptainObvliousどのパラメータを並べ替えるのですか? – Bcmonks

答えて

0

あなたはstd::sortstd::setに供給することができます厳しい弱い順序付けを満たすカスタムコンパレータ(ファンクター、ラムダ、またはoperator <オーバーロード)が必要です。最も簡単な1次のようになります。

#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を作成します。

関連する問題