gpt4 book ai didi

c++ - 去除重复项的有效方法

转载 作者:太空宇宙 更新时间:2023-11-03 10:29:08 24 4
gpt4 key购买 nike

按照此线程“What's the most efficient way to erase duplicates and sort a vector?”中的答案。我写了下面的代码,但我收到了一个错误提示 no match for ‘operator<’ (operand types are ‘const connector’ and ‘const connector’)废话……

connector是我自己写的一个类,它基本上是一条有两个几何点的线。 uniqCntrs是一个 std::vector。它有 100% 的重复项,这意味着每个元素都有一个重复项,大小为 uniqCntrs。相当大。我的代码有什么问题,如何处理这种情况?

std::set<connector> uniqCntrsSet;
for(unsigned int i = 0; i < uniqCntrs.size(); ++i )
{
uniqCntrsSet.insert(uniqCntrs[i]);
}
uniqCntrs.assign(uniqCntrsSet.begin(), uniqCntrsSet.end());

编辑:

我不知道如何定义 <我的连接器类的运算符。我的意思是说一条线比另一条线在物理上没有意义。

最佳答案

来自 cppreference :

std::set is an associative container that contains a sorted set of unique objects of type Key. Sorting is done using the key comparison function Compare.

std::set 的第二个模板参数, Compare , 默认为 std::less 默认情况下将对象与 operator< 进行比较.要解决此问题,您可以简单地定义 operator<为你的Key输入( connector 即)。

关于c++ - 去除重复项的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22155623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com