[英]How to apply std::sort and std::erase on struct of CvPoint and int?
我有一個CvPoint和int的結構,如下所示。
struct strcOfPoints {
CvPoint p;
int c;
};
而且我有一個這樣的向量strcOfPoints
UniquePoints::strcOfPoints s1, s2, s3, s4, s5, s6, s7;
s1.p = { 33, 122 }, s1.c = 255;
s2.p = { 68, 207 }, s2.c = 158;
s3.p = { 162, 42 }, s3.c = 120;
s4.p = { 219, 42 }, s4.c = 50;
s5.p = { 162, 216 }, s5.c = 20;
s6.p = { 162, 216 }, s6.c = 20;
s7.p = { 162, 216 }, s7.c = 20;
vector <UniquePoints::strcOfPoints> strpts = { s1, s2, s3, s4, s5, s6, s7 };
問 :我們如何在結構的向量上應用std :: sort和std :: erase以刪除重復的點?
注意。 我可以用點向量來做。 但是我無法為Point和int的結構向量做。
需要指導以進一步處理。 謝謝
首先,讓我們建立一個比較器:
bool compare(strcOfPoints const & lhs, strcOfPoints const & rhs) {
return std::tie(lhs.p, lhs.c) < std::tie(rhs.p, rhs.c);
}
接下來,我們對向量進行排序:
vector<strcOfPoints> strpts = { s1, s2, s3, s4, s5, s6, s7 };
std::sort(strpts.begin(), strpts.end(), compare);
最后,我們刪除所有重復項:
strpts.erase(
std::unique(strpts.begin(), strpts.end(), compare),
strpts.end());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.