c++使用指针将字符串进行排序。。我想知道我哪里除了小错误?这次是可以编译但是运行错误
#include<iostream>#include<string>usingnamespacestd;intmain(){strings[4]={"Africa","A...
#include <iostream>
#include <string>
using namespace std;
int main(){
string s[4] = {"Africa","Asia","America","Europe"};
string *p1;
string *p2;
string *t;
string temp;
for(p1 = s;p1<=(s+2);p1++){
t = p1;
for(p2 = (p1+1);p2<=(s+3);p2++)
if(*t>*p2) t = p2;
temp = *t; *t = *p2; *p2 = temp;
}
for(p1 = s;p1<=(s+3);p1++){
cout<<*p1<<endl;
}
return 0;
} 展开
#include <string>
using namespace std;
int main(){
string s[4] = {"Africa","Asia","America","Europe"};
string *p1;
string *p2;
string *t;
string temp;
for(p1 = s;p1<=(s+2);p1++){
t = p1;
for(p2 = (p1+1);p2<=(s+3);p2++)
if(*t>*p2) t = p2;
temp = *t; *t = *p2; *p2 = temp;
}
for(p1 = s;p1<=(s+3);p1++){
cout<<*p1<<endl;
}
return 0;
} 展开
1个回答
展开全部
#include <iostream>
#include <string>
using namespace std;
int main(){
string s[4] = {"Africa","Asia","America","Europe"};
string *p1;
string *p2;
string *t;
string temp;
for(p1 = s;p1<=(s+2);p1++){
t = p1;
for(p2 = (p1+1);p2<=(s+3);p2++)
if( strcmp(t->c_str(),p2->c_str()) >0 )
t = p2;
temp = *t; *t = *p1; *p1 = temp;
}
for(p1 = s;p1<=(s+3);p1++){
cout<<*p1<<endl;
}
return 0;
}
#include <string>
using namespace std;
int main(){
string s[4] = {"Africa","Asia","America","Europe"};
string *p1;
string *p2;
string *t;
string temp;
for(p1 = s;p1<=(s+2);p1++){
t = p1;
for(p2 = (p1+1);p2<=(s+3);p2++)
if( strcmp(t->c_str(),p2->c_str()) >0 )
t = p2;
temp = *t; *t = *p1; *p1 = temp;
}
for(p1 = s;p1<=(s+3);p1++){
cout<<*p1<<endl;
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询