목록 CPP에서 중복 삭제

NNguyen

구조 목록을 작성 중이지만 중복 요소를 제거하는 방법을 모릅니다. -여기서 구조는 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;
}

`

Vlad / 모스크바

우리 초보자는 서로 도와야하지 않습니까? :)

목록에 중복되지 않은 목표를 달성하기위한 세 가지 접근 방식이 있습니다.

첫 번째는 목록에 값이있는 요소가 이미없는 경우에만 목록에 새 값을 삽입하는 것입니다.

두 번째는 목록을 정렬하고 고유 한 방법을 적용하는 것입니다.

세 번째는 두 개의 루프를 사용하는 것입니다.

적어도 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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

목록 목록에서 중복 제거

분류에서Dev

목록 목록에서 중복 제거

분류에서Dev

CSV에서 중복 항목 삭제

분류에서Dev

중첩 목록에서 중복 제거

분류에서Dev

중첩 목록에서 중복 제거

분류에서Dev

Python의 목록 목록에서 중복 목록 제거

분류에서Dev

튜플 목록에서 중복 제거

분류에서Dev

URL 목록에서 중복 제거

분류에서Dev

C # 목록에서 중복 제거 <string []>

분류에서Dev

목록 요소에서 중복 제거

분류에서Dev

Python, 목록에서 중복 제거

분류에서Dev

Python : 목록에서 중복 제거

분류에서Dev

목록에서 중복 세트 제거

분류에서Dev

부울 목록에서 중복 제거

분류에서Dev

Python : 목록 목록에서 중복 항목 개수 및 제거

분류에서Dev

이중 연결 목록-메모리에서 목록 삭제

분류에서Dev

파이썬 : 목록에서 중복 문자가있는 모든 단어 삭제

분류에서Dev

df / 목록 목록에서 특정 중복 제거

분류에서Dev

R의 목록 목록에서 중복을 삭제하는 가장 좋은 방법

분류에서Dev

이중 연결 목록에서 첫 번째 항목 삭제

분류에서Dev

중첩 된 <ul> 목록 항목에서 테이블 행 삭제

분류에서Dev

중첩 된 <ul> 목록 항목에서 테이블 행 삭제

분류에서Dev

postgres에서 중복 삭제

분류에서Dev

SQLBase에서 중복 삭제

분류에서Dev

Excel 중복 항목 삭제

분류에서Dev

중복 항목 삭제

분류에서Dev

중복 기록 삭제

분류에서Dev

목록 목록에서 항목 삭제

분류에서Dev

Python Dataframe : 특정 조건에서 중복 항목 삭제