2016-01-24 117 views
-1

我有两个数组,一个用于点,另一个用于保存名称。我想按升序对数组进行排序,并按名称数组对其进行排序,以与相应的点数组一起移动。我想知道我该怎么做。 这里是我到目前为止如何根据第一个数组排序两个数组

string sName[noS]; 
char sex[noS];   //Variable to hold the sex information of the students 
char gpa[noS];   //Variable to hold the GPA 
char essayGrade[noS]; 
int convGpa[noS]; 
int convEssayGrade[noS]; 
double overallPoint[noS]; 
pair<double, string> pairs[noS]; 
int want_len = noS; 



pairs[i] = make_pair(overallPoint[i], sName[i]); 
        cout << endl <<"Over all point of " << pairs[i].second << ": " << pairs[i].first << endl; 
        sort(pairs.begin(), pairs.end()); 

所有必需的变量都被初始化尝试。

+0

您是否考虑过使用'std :: map'而不是2个数组? –

+0

你能告诉我我该如何实施?我不认为我可以这样做,因为overallPoints不是唯一的。 –

+0

如果键不唯一,可以使用'std :: multimap'。 –

回答

0

如果我有你的权利,你可以使用(图):

 #include<map> 
     using namesapce std; 
     map<double, string> myMap; 
     myMap.insert(name of the pair you want to insert) 

的std ::地图排序,自automaticaly

+0

返回什么 –

+0

@Bereket您将拥有一个按键关联容器排序(在您的案例中:number,name),您可以在这里阅读std :: map:http://www.cplusplus.com/reference/map/map/||对于具有相同值的多键,您可以使用std :: multimap – Sunz

+0

正如我之前在问题的评论中所说的那样,double值可能不是唯一的。 –

0

偷懒的做法=>使用std :: stable_sort。参见:http://en.cppreference.com/w/cpp/algorithm/stable_sort

不那么懒惰的方法=>重写你的设计。有一个包含名称和点信息的结构数组。 (合并此信息)根据使用点成员的比较函数对此结构数组进行排序。

如果你坚持使用这种设计,我会写我自己的小排序函数,也会交换名称数组的位置,同时交换点的位置......但这不是很好吗?

相关问题