我想按照字母顺序排列名称的二维数组,但我无法缝合以使其工作。按字母顺序排列2D Char阵列?
我在字母上使用了气泡排序,这是对名称的第一个字母进行排序,但是3个名字以相同的字母开始,并且它们仍然没有顺序。
我试图googleing之类的东西,但每婷说,使用向量或字符串变量..但我仅限于使用二维字符数组..
任何想法?
下面是代码我此刻的作品近:
using namespace std;
int main(){
char heroes[11][17] = { "Captain America", "Thor", "Wolverine", "Cyclops", "Goliath", "Beast", "Angel", "Colossus", "Hulk", "Quicksilver", "Ironman"};
cout<<"Printing the array as is"<<endl<<endl;
for (int i=0; i<12; i++){
cout<<heroes[i]<<endl;
}
cout<<endl<<"Ordering the heroes in Alphabetical order"<<endl<<endl;
char temp = NULL;
// bubble sort
for(int i=0;i<11;i++){
for(int j=0; j<(11-1); j++){
if (heroes[i][0] < heroes[j][0]){
for (int k=0; k<17-1; k++){
swap(heroes[i][k], heroes[j][k]);
}
}
}
}
cout<<"Printing the array Sorted"<<endl<<endl;
for (int i=0; i<12; i++){
cout<<heroes[i]<<endl;
}
// Pause
cout<<endl<<endl<<endl<<"Please Close Console Window"<<endl;
cin.ignore('\n', 1024);
return(0);
}
好吧我得到它的工作!
这里是代码...(我如何这种形式邮编BTW?)
它几乎是完全相同德一样,但使用完整的字符串比较和副本。
你被允许使用'strcmp'? – 2012-03-17 12:50:40