在2D平面找到重复点
Finding repeated points in 2D plane
我试图从给定的10个点找到重复点,其中每个点具有x和y值。我写了以下代码,但无法获得正确的结果。输出应为{3,5},{4,2},{2,4},{7,8}
#include <iostream>
#include<stdlib.h>
using namespace std;
struct point
{
int x;
int y;
};
void distinctPoints(point arr[], int size)
{
cout<<"Repeated Points"<<endl;
cout<<"x, y"<<endl;
for(int i = 0; i< size; i++)
for(int j = i+1; j< size; j++)
{
if ((arr[i].x==arr[j].x) && (arr[i].y==arr[j].y))
{
cout<<arr[j].x <<", "<<arr[j].y<<endl;
break;
}
}
}
int main()
{ int size=10;
point points[size]={{3,5},{4,2},{2,4},{3,5},{7,8},{7,8},{4,2},{7,8},{3,5},{2,4}};
distinctPoints(points, size);
return 0;
}
您的方法(一旦纠正,就像VHS的答案一样)可能可以点数少数点,但是,使用较大的数据,o(n 2 2 )算法可能太低效率。
您可以利用std::unordered_set
中插入元素的平均固定时间,即使您需要编写类比较功能和班级的哈希功能。
下面介绍的算法使用两个unordered_set:
-
uniques
最终存储源容器中存在的所有元素,而无需重复。 -
repeated
仅存储多次出现的元素的唯一实例。
仅在uniques
中已经存在的元素才会复制到输出,但在repeated
中不存在。
#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>
#include <iterator>
struct point
{
int x, y;
bool operator== (point const& b) const
{
return x == b.x && y == b.y;
}
};
namespace std {
template<> struct hash<point>
{
std::size_t operator() (point const& p) const
{
return (std::hash<int>{}(p.x) << 1) ^ std::hash<int>{}(p.y);
}
};
}
std::ostream& operator<< (std::ostream& os, point const& pt)
{
return os << '(' << pt.x << ", " << pt.y << ')';
}
template<class InputIt, class OutputIt>
OutputIt copy_repeated_values(InputIt first, InputIt last, OutputIt dest)
{
using value_type = typename InputIt::value_type;
std::unordered_set<value_type> uniques, repeated;
return std::copy_if(
first, last, dest, [&] (value_type const& value) {
return
not uniques.insert(value).second &&
repeated.insert(value).second;
}
);
}
int main()
{
std::vector<point> points {
{3,5}, {4,2}, {2,4}, {3,5}, {7,8}, {7,8}, {4,2}, {7,8}, {3,5}, {2,4}
};
copy_repeated_values(
std::begin(points), std::end(points),
std::ostream_iterator<point>(std::cout, " ")
);
std::cout << 'n';
}
输出为:
(3,5)(7,8)(4,2)(2,4)
我已经调整了您的distinctPoints
方法,以便即使DUP出现超过两次,它也不会多次打印重复项。请参阅以下编辑:
void distinctPoints(point arr[], int size)
{
point dups[size];
cout<<"Distinct Points"<<endl;
cout<<"x, y"<<endl;
for(int i = 0; i < size; i++)
for(int j = 0; j < size; j++) {
if ((arr[i].x==arr[j].x) && (arr[i].y==arr[j].y)) {
if(j < i) {
break;
}
else if( j == i) {
continue;
}
else {
cout<<arr[i].x <<", "<<arr[i].y<<endl;
break;
}
}
}
}
这应该做您要实现的目标,我正在使用C 中的集合和地图,以照顾独特的条目。
该地图跟踪已经访问的点。
#include <iostream>
#include<stdlib.h>
#include <set>
#include <map>
using namespace std;
struct point
{
int x;
int y;
};
map<pair<int, int>, int> mapCountOfPoints;
set<pair<int, int> > disPoints;
void distinctPoints(point arr[], int size)
{
for(int i=0; i<size; i++) {
pair<int, int> temp = make_pair(arr[i].x, arr[i].y);
if(mapCountOfPoints.find(temp) != mapCountOfPoints.end()) {
disPoints.insert(temp);
} else {
mapCountOfPoints[temp] = 1;
}
}
// Now we will iterate over the set to get the distinct set of points
for(set<pair<int, int>>::iterator it=disPoints.begin(); it!=disPoints.end(); it++) {
cout<<it->first<<" "<<it->second<<endl;
}
}
int main()
{ int size=10;
point points[size]={{3,5},{4,2},{2,4},{3,5},{7,8},{7,8},{4,2},{7,8},{3,5},{2,4}};
distinctPoints(points, size);
return 0;
}
希望这会有所帮助!
相关文章:
- 2D数组来自文本输入,中间有空格
- 将值指定给向量(2D)的向量中的某个位置
- 如何使用用户输入在C++中正确填充2D数组
- 如何在C++中检查2D数组中负值的输入验证
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 在 2D 向量中使用第三个 [ ] 有什么意义?
- 四边形的 2D 旋转
- 打印第二列时的2d字符矢量打印空间
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 如何在C++函数中声明静态 2D 数组?
- 我是 C++ 的初学者,我想知道如何在 2D 矢量中获取重复值
- 如何声明一个可以在整个程序中使用的全局 2d 3d 4d .. 数组(堆版本)变量?
- opengl glBegin(GL_LINES) 和 glBegin(GL_POINT) 在 2D 中不可视化点矢量
- 在C++中迭代 2D 容器的最干净方法
- 如何引用 2D 指针?
- 如何在 C++ 中使用它的构造函数初始化 unique_ptrs 的 2D 向量?
- C++动态安全 2D 交错阵列
- 如何在C++或OpenCV子矩阵中的2D子向量上使用OpenACC?
- 在2D平面找到重复点
- OpenCV立体视觉3D坐标到2D相机平面投影不同于将2D点三角化到3D