如何从矢量中删除重复矩形<Rect>?

how to remove repeat rectangle from vector<Rect>?

本文关键字:lt Rect gt 删除      更新时间:2023-10-16

我在vector<Rect>中存储了许多Rect rectangle。但它内部有许多重复的矩形。如何移除它们?例如:

    Point Pt1(267, 83);                 
    Point Pt2(487, 167);
    Rect    rec1(Pt1, Pt2);
    GroundTruthSet.push_back(rec1);

    Point Pt3(257, 90);
    Point Pt4(450, 150);
    Rect    rec2(Pt3, Pt4);
    GroundTruthSet.push_back(rec2);
    Point Pt5(267, 83);                 
    Point Pt6(487, 167);
    Rect    rec3(Pt1, Pt2);
    GroundTruthSet.push_back(rec3);

如何删除vector<Rect>中的重复矩形?

您需要在Rect上创建一个严格弱排序。对于矩形,比较它们各自的组件就足够了。

auto comp_lt = [](const Rect& lhs, const Rect& rhs) {
    // compare each component in the following lexicographical order
    return std::tie(lhs.x, lhs.y, lhs.width, lhs.height) <
        std::tie(rhs.x, rhs.y, rhs.width, rhs.height);
};
auto comp_eq = [](const Rect& lhs, const Rect& rhs) {
    // `std::unique` uses equality-comparison, not less-than-comparison
    return std::tie(lhs.x, lhs.y, lhs.width, lhs.height) ==
        std::tie(rhs.x, rhs.y, rhs.width, rhs.height);
};
std::sort(std::begin(GroundTruthSet), std::end(GroundTruthSet), comp_lt);
auto pivot = std::unique(std::begin(GroundTruthSet), std::end(GroundTruthSet), comp_eq);
v.erase(pivot, std::end(GroundTruthSet));

std::sortstd::uniquestd::tie