2014-02-12 28 views
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; 
+1

使用您發現的衆多算法之一? –

+1

(0,0),(0,1),(1,0),(1,1)的模式是什麼? –

+0

所有點均等(一次),因此上述數據集中沒有模式。 – askingtoomuch

回答

1

因爲==不受struct Points超載你得到了錯誤。

兩個解決方案:

  1. 超載==運營商struct Points

    typedef struct Points{ 
        int x; 
        int y; 
    
        bool operator==(const Points& a) const 
        { 
         return (x == a.x && y == a.y); 
        } 
    }Point; 
    
  2. 更換

    if (myPoint[i] == currpoint) 
    

    if (myPoint[i].x==currpoint.x && myPoint[i].y==currpoint.y) 
    
+0

非常感謝你!多麼愚蠢的錯誤! – askingtoomuch