c++ - 如何删除结构 vector 的重复项 C++

我有 vector 运动

 vector<posToMove> movements;

posToMove 是一个结构体:

struct posToMove
{
    int fromX;
    int fromY;
    int toX;
    int toY;
};

我想删除移动中的重复项,我该怎么做?

最佳答案

最简单的方法是使用:

movements.erase(std::unique(movements.begin(), movements.end()), movements.end());

但是std::unique只删除连续 重复的元素,因此您需要对 std::vector 进行排序首先,通过重载 <==运算符(operator):

struct posToMove
{
    int fromX;
    int fromY;
    int toX;
    int toY;

    bool operator < (const posToMove& other) const
    {
        //declare how 2 variable of type posToMove should be compared with <
        return std::make_tuple(fromX, fromY, toX, toY) < std::make_tuple(other.fromX, other.fromY, other.toX, other.toY);
    }

    bool operator == (const posToMove& other) const
    {
        //declare how 2 variable of type posToMove should be compared with ==
        return std::make_tuple(fromX, fromY, toX, toY) == std::make_tuple(other.fromX, other.fromY, other.toX, other.toY);
    }
};

我声明了<==运算符 make_tuple() , 但您也可以将其替换为您选择的比较。

代码:

#include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>
struct posToMove
{
    int fromX;
    int fromY;
    int toX;
    int toY;

    bool operator < (const posToMove& other) const
    {
        //declare how 2 variable of type posToMove should be compared with <
        return std::make_tuple(fromX, fromY, toX, toY) < std::make_tuple(other.fromX, other.fromY, other.toX, other.toY);
    }

    bool operator == (const posToMove& other) const
    {
        //declare how 2 variable of type posToMove should be compared with ==
        return std::make_tuple(fromX, fromY, toX, toY) == std::make_tuple(other.fromX, other.fromY, other.toX, other.toY);
    }
};

std::vector<posToMove>movements;

int main()
{
    movements.push_back({0,1,0,0});
    movements.push_back({1,2,5,7});
    movements.push_back({3,9,9,6});
    movements.push_back({0,1,0,0});
    movements.push_back({4,1,8,0});
    movements.push_back({1,2,5,7});

    std::sort(movements.begin(), movements.end());

    std::cout << "After sort : \n";
    for (auto x : movements)
    {
        std::cout << x.fromX << " " << x.fromY << " " << x.toX << " " << x.toY << "\n";
    }
    std::cout << "\n";

    movements.erase(std::unique(movements.begin(), movements.end()), movements.end());

    std::cout << "After removing : \n";
    for (auto x : movements)
    {
        std::cout << x.fromX << " " << x.fromY << " " << x.toX << " " << x.toY << "\n";
    }
}

结果:

After sort :
0 1 0 0
0 1 0 0
1 2 5 7
1 2 5 7
3 9 9 6
4 1 8 0

After removing :
0 1 0 0
1 2 5 7
3 9 9 6
4 1 8 0

相关:Remove duplicates in vector of structure c++

文档:

  • erase() : https://en.cppreference.com/w/cpp/container/vector/erase2

  • std::unique : https://en.cppreference.com/w/cpp/algorithm/unique

  • std::sort : https://en.cppreference.com/w/cpp/algorithm/sort

  • 重载运算符:https://en.cppreference.com/w/cpp/language/operators

https://stackoverflow.com/questions/67929680/

相关文章:

r - 将数据帧的每一行乘以它的向量 R

vuejs3 - react 对象未在模板 Vue3 Composition API 上更新

docker - docker compose up 后后端到 redis 连接被拒绝

android - 臭名昭著的高度 :100% issue on chrome for androi

javascript - 如何从标称字符串中删除 0,但不从 javascript 中的小数中删除

r - 从单个表中查找多列

java - 如何在 Java 数组上设置新的属性或方法?

bash - 在组合前面的数据的同时在多行中添加值

javascript - 计算数组中每个元素的频率 - javascript

html - 如何使整个 HTML 日期字段可点击?