点坐标数据集中的模式
Mode in point coordinate data set
我有一组点坐标(x,y)存储在一个向量中。我想在向量中找到模式(最常见的点)。我发现很多算法如何计算模式,但是如何在点坐标数据集中找到模式?
typedef struct Points{
int x;
int y;
}Point
vector<Points> myPoint;
例如,myPoint: (0,0), (2,3), (6,2), (2,3), (4,1), ...模式为 (2,3),
Points currpoint = myPoint[0];
int counter = 1;
int maxcounter = 1;
Points mode = myPoint[0];
int n = myPoints.size();
for (int i = 1; i < n; ++i) {
if ( myPoint[i] == currpoint ) // !!! "==" produces error
++counter;
else { // next point started...
if (counter > maxcounter) { // new mode candidate
maxcounter = counter;
mode = currvalue;
}
currpoint = myPoint[i]; // ready to count next point
counter = 1;
}
}
cout << "Mode: " << mode << endl;
"=="操作数产生错误,我通过比较结构"点"来做正确的方法吗?
[已更正]
Points currpoint = myPoint[0];
int counter = 1;
int maxcounter = 1;
Points mode = myPoint[0];
int n = myPoints.size();
for (int i = 1; i < n; ++i) {
if ( myPoint[i].x == currpoint.x && myPoint[i].y == currpoint.y )
++counter;
else {
if (counter > maxcounter) {
maxcounter = counter;
mode = currvalue;
}
currpoint = myPoint[i];
counter = 1;
}
}
if (counter > maxcounter) {
maxcounter = counter;
mode = currvalue;
}
cout << "Mode: " << mode.x <<", "<< mode.y << endl;
您收到错误是因为==
没有被struct Points
重载。
两种解决方案:
-
struct Points
的过载==
操作员:typedef struct Points{ int x; int y; bool operator==(const Points& a) const { return (x == a.x && y == a.y); } }Point;
-
取代
if ( myPoint[i] == currpoint )
自
if ( myPoint[i].x==currpoint.x && myPoint[i].y==currpoint.y)
相关文章:
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 为什么在保护模式下继承升级不起作用
- 如何在全屏模式下(在OpenGL中)使背景透明
- 为什么使用__LINE_的代码在发布模式下在MSVC下编译,而不是在调试模式下
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- 如何从存储在std::映射中的std::集中删除元素
- 此模式的C++RegEx
- avrogencpp能为模式中的每种类型生成单独的头文件吗
- 使用可变模板的Broadcaster/Listener模式
- c++方法参数只能在linux的发布模式下自行更改
- 资源管理设计模式
- 使用 mod_gsoap 部署服务时,如何在 Gsoap 中更改 soap 上下文的模式?
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 将非平凡的项目放在多集中
- 小字符串优化(调试与发布模式)
- 可视化C++:发布模式的运行时库作为'Multi-threaded Debug DLL'
- 如何设计具有不同类型的通知和观察器的观察者模式?
- 在C++的一系列数字中查找重复模式
- 点坐标数据集中的模式
- 在新托管线程中通过 C# 在混合模式程序集中调用 STL 矢量的push_back