2017-07-27 21 views
0

私は最後の2時間、これを頭で叩いていました。私はオペレータ==とネストされたラムダを実装しようとしました。ここ複雑な構造を比較する

は、構造体(複数のネストされた構造体)である:

struct Deepest{ 
    int att1; 
    int att2; 
}; 

struct Useless{ 
    int u1; 
    int u2; 
}; 

struct Leveli { 
    std::vector<Deepest> deeps; 
    Useless extra; 
}; 

struct Top{ 
    std::vector<Leveli> multilev; 
    int cost; 
}; 

いくつかのint型の属性は、実際に列挙されているが、それは問題には関係ないはず。 基本的にはTopオブジェクトを設定していて、いったん作成されると、Deepestのベクトルのみに基づいて、重複したLeveliを特定して削除したいと考えています(私は無用な余分なものを比較したくありません)。ここで私は仕事をするためにしようとしているコードは次のとおりです。LHSでラムダを交換する際

auto sub_comp = [](const Deepest& lhs, const Deepest& rhs) {return (lhs.att1== rhs.att1&& lhs.att2== rhs.att2); }; 
    auto comp = [&](const Leveli& lhs, const Leveli& rhs) {return std::equal(lhs.deeps.begin(), lhs.deeps.end(), rhs.deeps.begin(), sub_comp); }; 
    std::sort(mytop.multilev.begin(), mytop.multilev.end()); 
    auto last = std::unique(mytop.multilev.begin(), mytop.multilev.end(), comp); 
    mytop.multilev.erase(last, mytop.multilev.end()); 

コンパイルは==私にオーバーロードされた関数や演算子がありませんというエラーの束を与える== rhsの

は多分それがされますこの方法では不可能な場合は、両方のレベルのベクトルを手作業でループして検索を実行するか、データ構造を確認する必要があります。

ありがとうございます!

最終解決:

struct Deepest{ 
    int att1; 
    int att2; 

    bool operator==(const Deepest& rhs) { 
     return att1 == rhs.att1 && att2 == rhs.att2; 
    } 

    bool operator<(const Deepest& rhs) { 
     return att1 < rhs.att1; 
    } 
}; 

struct Useless{ 
    int u1; 
    int u2; 
}; 

struct Leveli { 
    std::vector<Deepest> deeps; 
    Useless extra;  

    bool operator==(const Leveli& rhs) { 
     return std::equal(deeps.begin(), deeps.end(), rhs.deeps.begin());  
    }  

    bool operator<(const Leveli& rhs) { 
     return deeps.size() < rhs.deeps.size(); 
    } 
}; 


struct Top{ 
    std::vector<Leveli> multilev; 
}; 

テストで:

std::sort(strategy.rules.begin(), strategy.rules.end()); 
auto last = std::unique(strategy.rules.begin(), strategy.rules.end()); 
strategy.rules.erase(last, strategy.rules.end()); 

警告!!: ベクトルディープス(STD ::ベクトル)押し戻す前(のstd ::ソート)をソートする必要がありますmultilevベクトルのLeveliオブジェクト

答えて

1

私は正常にこれらすべてをテストすることができましたし、このケースでは、私はカスタム述語の上に演算子のオーバーロードを使用して好む。また、あなたのプログラムの作業

struct Deepest { 
    int att1; 
    int att2; 
}; 

bool operator==(const Deepest& lhs, const Deepest& rhs) { 
    return lhs.att1 == rhs.att1 && lhs.att2 == rhs.att2; 
} 

struct Useless { 
    int u1; 
    int u2; 
}; 

struct Leveli { 
    std::vector<Deepest> deeps; 
    Useless extra; 
}; 

bool operator==(const Leveli& lhs, const Leveli& rhs) { 
    return std::equal(lhs.deeps.begin(), lhs.deeps.end(), rhs.deeps.begin()); 
} 

bool operator<(const Leveli& lhs, const Leveli& rhs) { 
    // You need to implement operator< for your sort to work. 
} 
+0

を作るのに十分でなければなりません。あなたのクラス/構造体はすぐに使えます! –

+0

ありがとう!私は本当に近くになった、私は演算子が欠けていた<仕事の種類のために!しかし、私は演算子を構造体の中にオーバーロードする必要がありました(さもなければ、私は再定義された演算子に関するリンカーエラーを得るでしょう) –

+0

私は私の最終的な解決策で私の質問を更新しました:) –