【问题标题】:how to remove duplicates of a vector of structures c++如何删除结构向量的重复项c ++
【发布时间】:2021-06-11 08:25:05
【问题描述】:

我有矢量运动

 vector<posToMove> movements;

posToMove 是一个结构:

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

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

【问题讨论】:

  • 你能对向量进行排序吗?
  • 为您的结构类型编写等价和比较器支持,对其进行排序,然后使用v.erase(std::unique(v.begin(), v.end()), v.end());
  • @WhozCraig 什么是等效和比较器支持?比如,我该怎么做?

标签: c++ vector


【解决方案1】:

最简单的方法是使用:

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

std::unique 只删除连续 个重复的元素,所以你需要先对std::vector 进行排序,通过重载&lt;== 运算符:

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);
    }
};

我将 &lt;== 运算符声明为 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++

文档:

【讨论】:

    猜你喜欢
    • 2014-11-06
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-12
    • 1970-01-01
    • 2022-01-22
    • 2016-04-01
    相关资源
    最近更新 更多