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

如题所述

第1个回答  推荐于2016-10-20
支持任意数目城市,c++代码:

#include <iostream>
#include <list>
#include <string>
using namespace std;

bool compare(string first, string second)
{
unsigned int i=0;
while ( (i<first.length()) && (i<second.length()) )
{
if (first[i]<second[i]) return true;
else if (first[i]>second[i]) return false;
++i;
}
if (first.length()<second.length()) return true;
else return false;
}
int main()
{
int n, i;
list<string> mylist;
list<string>::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;
}本回答被提问者采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网