use*_*514 9 c c++ sorting pointers
我正在以两种方式对我的汽车阵列进行排序.一年一年,如下所示.和另一个由make.Make是一个char*如果我只是指向它们,我如何比较字符串?
int i, j;
for(i=0; i<100; i++){
for(j=0; j<100-i; j++){
if(carArray[i]!=NULL && carArray[j]!= NULL && carArray[j+1]!=NULL){
if(carArray[i]->year > carArray[j+1]->year){
swap(carArray[j], carArray[j+1]);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
上述方法适用于int(年).如何使它适用于char指针?
T.E*_*.D. 30
在几乎任何一个中,方式是打电话strcmp.如果您的字符串(出于某种奇怪的原因)未终止NUL,则应使用strncmp.
但是,在C++中,你真的不应该在char数组中操作字符串,如果你可以合理地避免它.请std::string改用.