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