求C++编程题一道 输入10~20个城市名,按字母顺序输出城市清单。 在线等!

2024-12-20 03:58:00
推荐回答(1个)
回答1:

支持任意数目城市,c++代码:

#include
#include
#include
using namespace std;

bool compare(string first, string second)
{
unsigned int i=0;
while ( (i {
if (first[i] else if (first[i]>second[i]) return false;
++i;
}
if (first.length() else return false;
}
int main()
{
int n, i;
list mylist;
list::iterator it;
string city;
cout << "输入将要输入的城市总数: ";
cin >> n;
cout << "请输入" << n << "个城市:" << endl;
for(i = 0; i < n; i ++) {
cin >> city;
mylist.push_back(city);
}
mylist.sort(compare);

cout << "排序后: " << endl;
for (it=mylist.begin(); it!=mylist.end(); ++it)
cout << *it << endl;
return 0;
}