구조 목록을 작성 중이지만 중복 요소를 제거하는 방법을 모릅니다. -여기서 구조는 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 ==
Point 클래스에 대해 정의하는 것이 좋습니다 .
아래에는 세 번째 및 두 번째 접근 방식을 보여주는 데모 프로그램이 있습니다. 귀하의 표기법을 사용했으며 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;
}
프로그램 출력은 다음과 같습니다.
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
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다