0
我有一組存儲在矢量中的點座標(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;
使用您發現的衆多算法之一? –
(0,0),(0,1),(1,0),(1,1)的模式是什麼? –
所有點均等(一次),因此上述數據集中沒有模式。 – askingtoomuch