本帖最后由 bin554385863 于 2019-10-16 23:11 编辑 #include <iostream>
#include <algorithm>
#include <vector>
void _sort()
{
int n = 1;
std::cout << "请输入数字的个数" << std::endl;
std::cin >> n;
std::vector<int> vec;
char arg;
std::cout << "升序请输入l, 降序请输入g" << std::endl;
std::cin >> arg;
if (arg != 'l' && arg != 'g')
{
std::cout << "参数错误";
exit(1);
}
std::cout << "请输入要排序的数字" << std::endl;
for (size_t i = 0; i < n; i++)
{
int t = 0;
std::cin >> t;
vec.push_back(t);
}
switch (arg)
{
case 'l':
sort(vec.begin(), vec.end(), std::less<int>()); //升序排序
break;
case 'g':
sort(vec.begin(), vec.end(), std::greater<int>()); //降序排序
default:
break;
}
for (int i : vec)
{
std::cout << i << " ";
}
std::cout << std::endl;
}
int main(int argc, char const *argv[])
{
_sort();
return 0;
}
---------------------------------------------------------------------------------------------------------
E:\Users\86184\Documents\Code>c:\Users\86184\.vscode\extensions\ms-vscode.cpptools-0.25.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-0ukibrgw.dct --stdout=Microsoft-MIEngine-Out-1gtvnay5.w3q --stderr=Microsoft-MIEngine-Error-akvbthh3.ems --pid=Microsoft-MIEngine-Pid-cwckpbd2.ods "--dbgExe=E:\My Program\MinGW\bin\gdb.exe" --interpreter=mi
请输入数字的个数
9
升序请输入l, 降序请输入g
l
请输入要排序的数字
20 36 65 98 10 0 32 56 9
0 9 10 20 32 36 56 65 98
E:\Users\86184\Documents\Code> |