這是我的問題: 我有一個結構:如何將結構成員數組作爲參數傳遞給函數?
struct point
{
int x;
int y;
};
,然後我有一個數組:
for (int i = 0;i < n;i++)
{
arr[i].x=rand() % n + 1;
}
我所定義的quicksort
功能如下:
void quicksort(int *a, int left, int right);
和我想通過X
座標來排序點,所以我打電話給quicksort
:
quicksort(arr.x, 0, n-1);
這是錯誤消息:
error: request for member 'x' in 'arr', which is of non-class type 'point [(((unsigned int)(((int)n) + -0x000000001)) + 1)]'
很抱歉,如果這個問題是太愚蠢或先天存在缺陷的,事實是我是一個新手,我很願意學習儘可能多儘可能,我會非常感謝你的幫助!
[避免使用'rand'。(http ://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) – chris
你應該使用'std :: sort',並且定義一個自定義的比較器。 –
你不能用這種方式來切片結構(即使你可以,你不會想要的,因爲那樣會對x座標進行排序,並將它們與隨機的y座標進行排序,而不是與原來配對的y座標。) –