2014-10-28 23 views
-4

這是我的代碼當我嘗試對結構向量進行排序時,我在算法文件中收到20個錯誤。我不知道什麼是錯的?

#include <stdio.h> 
#include <cstdio> 
#include <math.h> 
#include <algorithm> 
#include <set> 
#include <struct.h> 
#include <vector> 
#include <functional> 

using namespace std; 

struct points { 
    int x,y; 
}; 

bool operator<(const points &p1, const points &p2) { 
    return p1.x<p2.x; 
}; 

vector<points> a(1000000); 
int i,n,closest; 

int main() { 

    scanf("%d\n",&n); 

    for (i=0; i<n-1; i++) { 
     scanf("%d %d\n",&a[i].x,&a[i].y); 
    } 

    sort(0,n-1,a); 

    return 0; 
} 

,我得到的大多是國家的錯誤「需要的間接指針操作數(‘詮釋’無效)。出了什麼問題?我想在矢量內的結構進行排序。我有使用運算符重載

+0

嘗試布爾運算符<(常規點p1,常數點p2)沒有& – 2014-10-28 16:32:00

+0

您嘗試過哪些調試? – mskfisher 2014-10-28 16:32:40

+0

謝謝,但它沒有工作 – user3572917 2014-10-28 16:32:55

回答

0

這不是你如何使用std::sort更換。!

sort(0, n-1, a); 

有:

sort(a.begin(), a.end()); 
+0

另外,在全局範圍內聲明'vector a(n);'main'內部,而不是'vector a(1000000);'。否則,您將排序您未從用戶讀取的元素。 – 2014-10-28 20:56:26

0
#include <iostream> 
#include <algorithm> 
#include <vector> 
#include <functional> 

using namespace std; 

struct points { 
    int x,y; 
}; 

bool operator<(const points &p1, const points &p2) { 
    return p1.x<p2.x; 
}; 

vector<points> a; 
int i,n,closest; 

int main() { 
    cin >> n; 

    for (i=0; i<n; i++) { 
     points p; 
     cin >> p.x >> p.y; 
     a.push_back(p); 
    } 

    sort(a.begin(), a.end()); 

    for(i=0; i<n; i++) 
     cout << a[i].x << ',' << a[i].y << endl; 

    return 0; 
} 
相關問題