设有若干个长度不超过100的字符串(含空格和tab字符),设计各字符串的输入、输出及按大小(按ASCII值)排序的函数,
主函数main中让用户输入字符串个数,再调用输入、排序及输出函数,完成各字符串的处理。
【输入形式】
3 (用户输入字符串个数)
What's this?
How old are you?
What? (用户输入的三个字符串)
【输出形式】
How old are you?
What's this?
What? (程序输出排序后的三个字符串)
【样例输入】
4
how
How old are you?
what's this?
good by
【样例输出】
How old are you?
good by
how
what's this?
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
int main()
{
int n;
std::cin >> n;
std::cin.get();
std::vector<std::string>v(n);
for (std::string& s : v)
{
std::getline(std::cin, s);
}
std::sort(v.begin(), v.end());
for (const std::string& s : v)
{
std::cout << s << std::endl;
}
return 0;
}
用gets逐个接收输入,存储到字符串数组,然后用冒泡排序,strcmp进行字符串大小比较即可
C++的话,全输入到std::string里,然后用std::sort,最后输出