求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;
}
#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;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询