我有一个排序功能,应该将所有从 3 个或更多字符开始的用户名放在数组的顶部,以及所有 3 个以下的用户名
if(wcslen((WCHAR*)playerNames[i]) < 3)
(都将是“-”)到底部并替换为<Unknown>
. 我尝试了以下方法,但是在替换"-"
为"<Unknown>"
和崩溃时设置了奇怪的调试器值。
char* playerNames[30] = { "Player1", "Player2", "Player3", "Player4", "Player5", "Player6", "-", "Player7", "-", "-", "-", "-", "Player8", "Player9", "Player10", "Player11", "Player12", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "Player14", "Player15" };
void sort(){
char* sorted[29];
int slot = 0;
for (int i = 0; i < 30; i++){
if (playerNames[i] != "-"){
if (i == slot){
sorted[slot] = playerNames[i];
}
else {
sorted[i] = playerNames[i];
}
slot++;
}
else if (playerNames[i] == "-"){
slot++;
}
}
for (int i = 0; i < 30; i++){
if (wcslen((WCHAR*)sorted[i]) < 3){
sorted[i] = "<Unknown>";
}
playerNames[i] = sorted[i];
}
}
这应该返回
Player1
Player2
Player3
Player4
Player5
Player6
Player7
Player8
Player9
Player10
Player11
Player12
Player13
Player14
Player15
<Unknown>
<Unknown>
<Unknown>
<Unknown>
<Unknown>
<Unknown>
<Unknown>
<Unknown>
<Unknown>
<Unknown>
<Unknown>
<Unknown>
<Unknown>
<Unknown>
<Unknown>
编辑:尝试了以下方法,但我仍然无法正常工作:
void sort(){
char* sorted[30];
int slot = 0;
for (int i = 0; i < 30; i++){
if (strcmp(playerNames[i], "-") != 0){
if (i == slot){
sorted[slot] = playerNames[i];
}
else {
sorted[i] = playerNames[i];
}
slot++;
}
else if (playerNames[i] == "-"){
slot++;
}
}
for (int i = 0; i < 30; i++){
if (strlen(sorted[i]) < 3){
sorted[i] = "<Unknown>";
}
playerNames[i] = sorted[i];
}
}