如何从 vector<Rect> 中删除重复矩形?
how to remove repeat rectangle from vector<Rect>?
我在 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
上创建一个 Strict Weak Ordering。对于矩形,比较它们的各个组件就足够了。
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));
我在 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
上创建一个 Strict Weak Ordering。对于矩形,比较它们的各个组件就足够了。
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));