[英]How to apply std::sort and std::erase on struct of CvPoint and int?
I have a struct of CvPoint and int as follows. 我有一个CvPoint和int的结构,如下所示。
struct strcOfPoints {
CvPoint p;
int c;
};
And i have a vector of strcOfPoints like this 而且我有一个这样的向量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 };
Q. How can we apply std::sort and std::erase on vector of struct to remove the duplicate points? 问 :我们如何在结构的向量上应用std :: sort和std :: erase以删除重复的点?
Note. 注意。 I can do it with vector of Points.
我可以用点向量来做。 but i am failed to do it for vector of structure of Point and int.
但是我无法为Point和int的结构向量做。
Need guidelines to process further. 需要指导以进一步处理。 Thanks
谢谢
First, lets build a comparator: 首先,让我们建立一个比较器:
bool compare(strcOfPoints const & lhs, strcOfPoints const & rhs) {
return std::tie(lhs.p, lhs.c) < std::tie(rhs.p, rhs.c);
}
Next we sort the vector: 接下来,我们对向量进行排序:
vector<strcOfPoints> strpts = { s1, s2, s3, s4, s5, s6, s7 };
std::sort(strpts.begin(), strpts.end(), compare);
Finally we erase any duplicates: 最后,我们删除所有重复项:
strpts.erase(
std::unique(strpts.begin(), strpts.end(), compare),
strpts.end());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.