2016-02-19 54 views
0

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

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

`

+0

的可能的复制[什么是删除重复和排序向量的最有效方法是什么?( http://stackoverflow.com/questions/1041620/whats-the-most-efficient-way-to-erase-duplicates-and-sort-a-vector) – iksemyonov

回答

0

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

有三种方法可以实现列表没有重复的目标。

第一个是只有在列表中没有值的元素时才在列表中插入一个新值。

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

而第三个是使用两个循环。

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

下面有一个演示程序,显示第三种和第二种方法。我用你的符号和假设,你不能使用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 
+0

很多弗拉德。你的代码有效! – NNguyen

+0

@NNguyen没有。:)不客气。 –