删除列表 CPP 中的重复项

Delete duplicate in list CPP

本文关键字:删除列 列表 CPP 删除      更新时间:2023-10-16

我正在构建一个结构列表,但我不知道如何杀死重复的元素。- 这里的结构是 Point{x,y}。对于主程序,我放了一些样本点。我期望的结果是1 2, 0 2, 1 3, 3 5, 4 5 (重复 0 2 已删除)

struct Point{
   int x;
   int y;
   Point(int inX, int inY) : x(inX), y(inY) {}
};
int main() 
{
  list<Point> mst;
  Point temp(0, 2);
  mst.push_back(temp);
  Point temp2(1, 2);
  mst.push_back(temp2);
  Point temp3(0, 2);
  mst.push_back(temp3);
  Point temp4(1, 3);
  mst.push_back(temp4);
  Point temp5(3, 5);
  mst.push_back(temp5);
  Point temp6(4, 5);
  mst.push_back(temp6);
  for (list<Point>::iterator out = mst.begin(); out != mst.end();++out) { 
     cout << (*out).x << " " << (*out).y << endl;     
  }

// kill duplicate (I DONT KNOW HOW)
  for (list<Point>::iterator out = mst.begin(); out != mst.end();++out) { 
      cout << (*out).x << " " << (*out).y << endl;    
  }
  return 0;
}

'

我们初学者应该互相帮助,不是吗:)

有三种方法可以实现清单不重复的目标。

第一种方法是,仅当列表中还没有具有该值的元素时,才在列表中插入新值。

第二个是对列表进行排序并应用唯一的方法。

第三个是使用两个循环。

最好为类点定义至少operator ==

下面是一个演示程序,显示了第三种和第二种方法。我使用了您的符号,并假设您不能使用 2011 C++。

#include <iostream>
#include <list>
#include <iterator>
struct Point
{
    int x;
    int y;
    Point(int inX, int inY) : x(inX), y(inY) {}
};
bool operator ==( const Point &a, const Point &b )
{
    return a.x == b.x && a.y == b.y;
}
bool operator <( const Point &a, const Point &b )
{
    return a.x < b.x || (  !( b.x < a.x ) && a.y < b.y );
}
int main() 
{
{    
    std::list<Point> mst;
    Point temp(0, 2);
    mst.push_back(temp);
    Point temp2(1, 2);
    mst.push_back(temp2);
    Point temp3(0, 2);
    mst.push_back(temp3);
    Point temp4(1, 3);
    mst.push_back(temp4);
    Point temp5(3, 5);
    mst.push_back(temp5);
    Point temp6(4, 5);
    mst.push_back(temp6);
    for ( std::list<Point>::iterator out = mst.begin(); out != mst.end(); ++out ) 
    { 
        std::cout << (*out).x << " " << (*out).y << std::endl;     
    }
    std::cout << std::endl;
    for ( std::list<Point>::iterator out = mst.begin(); out != mst.end(); ++out ) 
    {
        std::list<Point>::iterator in = out;
        for ( std::advance( in, 1 ); in != mst.end(); )
        {
            if ( ( *in ).x == ( *out ).x && ( *in ).y == ( *out ).y )
            {
                in = mst.erase( in );
            }
            else
            {
                std::advance( in, 1 );
            }
        } 
    }        
    for ( std::list<Point>::iterator out = mst.begin(); out != mst.end(); ++out ) 
    { 
        std::cout << (*out).x << " " << (*out).y << std::endl;     
    }
    std::cout << std::endl;
}
{    
    std::list<Point> mst;
    Point temp(0, 2);
    mst.push_back(temp);
    Point temp2(1, 2);
    mst.push_back(temp2);
    Point temp3(0, 2);
    mst.push_back(temp3);
    Point temp4(1, 3);
    mst.push_back(temp4);
    Point temp5(3, 5);
    mst.push_back(temp5);
    Point temp6(4, 5);
    mst.push_back(temp6);
    for ( std::list<Point>::iterator out = mst.begin(); out != mst.end(); ++out ) 
    { 
        std::cout << (*out).x << " " << (*out).y << std::endl;     
    }
    std::cout << std::endl;

    mst.sort();
    mst.unique( operator == );
    for ( std::list<Point>::iterator out = mst.begin(); out != mst.end(); ++out ) 
    { 
        std::cout << (*out).x << " " << (*out).y << std::endl;     
    }
    std::cout << std::endl;
}
    return 0;
}

程序输出为

0 2
1 2
0 2
1 3
3 5
4 5
0 2
1 2
1 3
3 5
4 5
0 2
1 2
0 2
1 3
3 5
4 5
0 2
1 2
1 3
3 5
4 5