0

I am building a list of structure but I do not know how to kill the duplicate elements. - The structure here is Point{x,y}. for the main program, I put some samples points. The result I expected is 1 2, 0 2, 1 3, 3 5, 4 5 (duplicate 0 2 deleted)

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

`

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
NNguyen
  • 113
  • 1
  • 6
  • Possible duplicate of [What's the most efficient way to erase duplicates and sort a vector?](http://stackoverflow.com/questions/1041620/whats-the-most-efficient-way-to-erase-duplicates-and-sort-a-vector) – iksemyonov Feb 19 '16 at 08:39

1 Answers1

0

We beginners should help each other should not we?:)

There are three approaches to achieve the aim that the list had not duplicates.

The first one is to insert a new value in the list only if there is no already an element with the value in the list.

The second one is to sort the list and apply method unique.

And the third one is to to use two loops.

It would be good to define at least operator == for the class Point.

Below there is a demonstrative program that shows the third and the second approaches. I used your notation and suppose that you can not use C++ 2011.

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

The program output is

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
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335