写了好久...因为函数搞错...
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
/*
输入一组英文单词,按字典顺序(大小写相同地位)
示例:
输入:Information Info Inform suite suit Suite
输出:Info Inform Information suit Suite
*/
int main(){
string Str, str, S[100],s[100];
getline(cin, Str);
Str+='#';
str.assign(Str);
//先转换为全小写,方便比较
for(int i=0; i<str.length(); i++){
if(str[i]>='A' && str[i]<='Z'){
str[i]=str[i]-'A'+'a';
}
}
int cnt = 0;
int st=-1, ed=-1;
int flag = 0;
for(int i=0; i<str.length(); i++){
char c = str[i];
if(!flag){
if(c != ' '){
flag = 1;
st = i;
}
}else{
if(c == ' '||c == '#'){
flag = 0;
ed = i;
s[cnt].assign(str,st,ed-st); //注:str.assign(s,2,5); 表示将s的第2位开始的5位赋值给str
S[cnt].assign(Str,st,ed-st);
cnt++;
}
}
}
//冒泡排序
for(int i=0; i<cnt; i++){
for(int j=0; j<cnt-i-1; j++){ //注:j<cnt-i-1
int k = s[j].compare(s[j+1]);
if(k > 0){
swap(s[j],s[j+1]);
swap(S[j],S[j+1]);
}
}
}
//输出排序后原数组
for(int i=0; i<cnt; i++){
cout << S[i] << ' ';
}
cout << endl;
return 0;
}