删除列表 CPP 中的重复项
Delete duplicate in list CPP
我正在构建一个结构列表,但我不知道如何杀死重复的元素。- 这里的结构是 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 ==
。
下面是一个演示程序,显示了第三种和第二种方法。我使用了您的符号,并假设您不能使用 2011 C++。
#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
相关文章:
- 从链接列表c++中删除一个项目
- C++如何通过用户输入删除列表元素
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 删除列表中的第n个元素
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 从gsl_matrix中删除列
- 从文件中删除单词列表的最快方法
- 如何删除C 圆形列表中最高发现元素旁边的元素
- 删除为列表分配的内存
- 删除对象列表中的类似项目
- 如何使用C 创建多列列表/向量
- 从大向量中删除字符串列表的有效方法
- 从 2D 数组中删除列C++
- 删除标准::列表中的重复项
- 删除循环列表中的节点时崩溃
- 删除std列表中的指针对象
- 列表控件按特定列删除项
- 正确删除std::列表中分配给其他地方的指针
- 在C++中删除整个列表
- 如何删除std::列表中的一半元素