如何在 CvPoint 和 int 的结构上应用 std::sort 和 std::erase
How to apply std::sort and std::erase on struct of CvPoint and int?
我有一个 CvPoint 和 int 的结构,如下所示。
struct strcOfPoints {
CvPoint p;
int c;
};
我有一个像这样的 strcOfPoint 向量
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 来删除重复的点?
注意。我可以用点向量来做到这一点。但是对于点和整数的结构向量,我未能做到这一点。
需要进一步处理的指南。谢谢
首先,让我们构建一个比较器:
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());
相关文章:
- 我们应该在使用 std::bind 应用之前检查一个不为空的函数吗?
- 由于调用 std::condition_variable 后参数无效而导致应用程序崩溃
- 将 std::bind 应用于带有参数 <boost::asio::ip::tcp::socket> 时出错?
- 从函数应用程序创建 std::vector
- 应用于整型类型的编译时优化 'std::isfinite()'
- 将函数标识符作为右值引用传递并对其应用 std::move()
- OMNeT++ cPacket as std::bitset 以应用 Reed-Solomon 编码
- 对 QMainWindow 使用 std::unique_ptr 时退出时出现 QT 应用程序分段错误
- STD ::在模板函数中应用于构造函数
- 如何在std :: tuple中的每个元素上应用constexpr函数
- 使用STD ::应用实验
- STD :: cin和Scanf()应用于字符串之间的区别
- 复制 std::vector,但将 lambda 应用于每个元素
- 防御性地应用 std::move 到平凡可复制的类型是否不可取
- 在GLFW应用程序中,std::cin的行为很奇怪
- "std::swap"应用于这些对象时会做什么?
- 开发C++应用程序,目标服务器有不同的std lib版本,最佳实践是什么
- 使用 std::variant 作为类成员并应用访问者
- 是否可以将std ::函数应用于特征矩阵的每个元素
- 使用 std::应用于可变参数包