fork(1) download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <map>
  4. #include <vector>
  5. #include <string>
  6. #include <functional>
  7. #include <algorithm>
  8. using namespace std;
  9.  
  10. vector<int> insertion_sort(const string &input)
  11. {
  12. vector<int> numbers;
  13. int num;
  14. istringstream iss(input);
  15. while (iss >> num)
  16. {
  17. numbers.push_back(num);
  18. }
  19. sort(numbers.begin(), numbers.end());
  20. return numbers;
  21. }
  22.  
  23. string trim(string str)
  24. {
  25. str.erase(0, str.find_first_not_of("\t\n\v\f\r "));
  26. str.erase(str.find_last_not_of("\t\n\v\f\r ") + 1);
  27. return str;
  28. }
  29.  
  30. void Sort()
  31. {
  32. vector<int> data;
  33.  
  34. string params;
  35. getline(cin, params);
  36. params = trim(params);
  37.  
  38. string numbers;
  39. getline(cin, numbers);
  40.  
  41. data = insertion_sort(numbers);
  42.  
  43. if (params == "DESCENDING")
  44. {
  45. // DESCENDING
  46. for (auto it = data.rbegin(); it != data.rend(); ++it)
  47. {
  48. cout << *it << " ";
  49. }
  50. }
  51. else
  52. {
  53. // ASCENDING
  54. for (auto it = data.begin(); it != data.end(); ++it)
  55. {
  56. cout << *it << " ";
  57. }
  58. }
  59. }
  60.  
  61. int main()
  62. {
  63. map<string, function<void()>> functions;
  64. functions["SORT"] = Sort;
  65. /*
  66.   functions["IS_SORTED"] = Is_Sorted;
  67.   functions["GETMAX"] = GetMax;
  68.   functions["APPEND"] = AppendSorted;
  69.   functions["GET_HISTOGRAM"] = GetHistogram;
  70. */
  71.  
  72. int num;
  73. cin >> num;
  74.  
  75. while (num > 0)
  76. {
  77. string func;
  78. cin >> func;
  79.  
  80. auto it = functions.find(func);
  81. if (it != functions.end())
  82. {
  83. it->second();
  84. }
  85. cout << endl;
  86.  
  87. --num;
  88. }
  89.  
  90. return 0;
  91. }
  92.  
Success #stdin #stdout 0.01s 5344KB
stdin
2
SORT
1 -2 13 24 -100
SORT DESCENDING
1 2 3 5 6
stdout
-100 -2 1 13 24 
6 5 3 2 1