如何删除矢量中的重复值,最后一个除外
How can i delete duplicate value in vector except last one
我在向量中有一组坐标(x,y,z(值,其中第一个值和最后一个值应该相同,但向量中还有另一个坐标,该坐标对于向量中的第一个和最后一个元素也很常见。 我想在不更改顺序的情况下删除矢量中的重复元素。
下面是我的矢量。
std::vector<std::vector<mi::math::Vector_struct<mi::Float32, 3> >> points;
如果我理解你的问题没错,你是说:
- 向量中的第一个和最后一个元素相等。
- 您希望删除这两者之间等于它们的所有元素。
如果是这种情况,您可以使用标准的删除+擦除习惯用法,但调整边界:
// We need at least two elements to safely manipulate the iterators like this, and
// while we're testing the size we might as well make sure there's at least one
// element that could be removed.
if (points.size() >= 3) {
// Removes all elements between the first and last element that are equal to
// the first element.
points.erase(
std::remove(points.begin() + 1, points.end() - 1, points.front()),
points.end() - 1
);
}
确保你#include <algorithm>
得到std::remove()
。
请注意,此代码正在比较外部向量。 如果要在每个内部向量上运行它,只需这样做(遍历points
并将此代码应用于每个内部向量(。 如果您需要删除多个内部向量中的重复项,请详细说明您的问题。
相关文章:
- 如何知道地图中的最后一个元素是否被删除?
- 如何删除矢量中的重复值,最后一个除外
- 删除链接列表中剩余的最后一个节点
- 避免在使用链接列表从 deque 中删除最后一个节点时出现内存泄漏
- C++:在进行切片时对迭代器的约定,特别是对于访问最后一个元素并最终将其删除
- std::字符串擦除以删除最后一个字母
- 如何检查字符串中的最后一个字符是否是某个字符并将其从字符串中删除?(C++)
- 在弦乐器中删除最后一个字符
- 代码问题.使用矢量删除相同的数字,除了它的最后一个入口
- 如何删除最后一个逗号
- 删除最后一个节点,并将尾部更新到最后一个节点
- 无法使用擦除方法删除矢量的最后一个元素
- 试图删除最后一个std ::向量元素时,程序会崩溃
- 如果地图擦除范围内的第一个和最后一个相等,则是否删除该元素
- 我不想要最后一个数字/值后面的逗号,如何删除它(c ++,循环)
- 从字符串中删除字符的第一个和最后一个实例
- 删除文件 C++ 中字符串中最后一个出现的字符
- 为什么OpenDDS在删除DataWriter时重新发送相同的最后一个数据
- 在删除双重链接列表中的最后一个元素时,请阅读访问违规行为
- 为什么 std::remove 不删除数组的最后一个元素