编写程序,输入若干个字符串。要求:(1)按字符串长度的大小升序输出各个字符串。(2)按字符串中字符的ASCLL码值大小升序输出各个字符串。为什么我输不出结果来???我该如何改呢?#include
#include
#include
#include
using namespace std;
//比较函数,用于排序
bool compare(string a, string b) {
//长度不一样的时候采用长度来排序
if (a.length() != b.length()) {
return a.length() < b.length();
}
//长度一样的时候采用ASCLL值排序
return a < b;
}
int main()
{vectorlist;
string inputString;
while (cin >> inputString) {
//结束标志,测试方便,可以注释掉
if (inputString == "0") {
break;
}
//加入到vector
list.push_back(inputString);
}
//排序,系统方法
sort(list.begin(), list.end(), compare);
//依次输出
for (unsigned int i = 0; i<list.size(); i++) {
cout << list[i] << endl;
}
return 0;
}
aaa
bbb
ccc
ddd
aa
bb
cc
dd
hello
yellow
hungry
0
aa
bb
cc
dd
aaa
bbb
ccc
ddd
hello
hungry
yellow
Press any key to continue
没发现问题。只是(2)你没有做。
第二问
bool compare(string a, string b) {
return a < b;
}
world
hello
girl
product
productive
reality
real
apple
about
pass
rain
sight
native
zoom
battle
tone
young
yellow
the
history
0
about
apple
battle
girl
hello
history
native
pass
product
productive
rain
real
reality
sight
the
tone
world
yellow
young
zoom
Press any key to continue