Mini sort en c++
From Tuxunix
Exemple
$ cat sort_apple_file jobs:0:40.7291 wozniak:1:0.5682 sculley:11:123.456 spindler:19:17.483 amelio:35:2.15895 ellison:94:143.25 $ ./small_sort < /dev/null $ ./small_sort < sort_apple_file amelio:35:2.15895 ellison:94:143.25 jobs:0:40.7291 sculley:11:123.456 spindler:19:17.483 wozniak:1:0.5682 $ ./small_sort -t ':' -k 2 < sort_apple_file jobs:0:40.7291 wozniak:1:0.5682 sculley:11:123.456 spindler:19:17.483 amelio:35:2.15895 ellison:94:143.25 $ ./small_sort -t ':' -k 3 < sort_apple_file wozniak:1:0.5682 amelio:35:2.15895 spindler:19:17.483 jobs:0:40.7291 sculley:11:123.456 ellison:94:143.25 $ ./small_sort -r -k 3 -t ':' < sort_apple_file ellison:94:143.25 sculley:11:123.456 jobs:0:40.7291 spindler:19:17.483 amelio:35:2.15895 wozniak:1:0.5682 $
1./* 2. * main.cpp for (self) 3. * 4. * Made by tdd 5. * Login <tdd@insia.org> 6. * 7. * Started on jeu 08 f<C3><A9>v 2007 12:01:21 tdd 8. * Last update jeu 08 f<C3><A9>v 2007 12:56:33 tdd 9.*/ 10. 11.#include <algorithm> 12.#include <iostream> 13.#include <iterator> 14.#include <string> 15.#include <vector> 16. 17.using std::cerr; 18.using std::cin; 19.using std::cout; 20.using std::endl; 21.using std::string; 22.using std::vector; 23. 24.typedef struct { 25. string data; 26. string first; 27. int second; 28. double third; 29.} Line; 30. 31.void cutLine(Line& line, char delim) { 32. string::iterator p = std::find(line.data.begin(), line.data.end(), delim); 33. line.first = ""; 34. std::copy(line.data.begin(), p, back_inserter(line.first)); 35. string::iterator p2 = std::find(++p, line.data.end(), delim); 36. string buf; 37. std::copy(p, p2, back_inserter(buf)); 38. line.second = strtol(buf.c_str(), 0, 0); 39. buf = ""; 40. std::copy(++p2, line.data.end(), back_inserter(buf)); 41. line.third = strtod(buf.c_str(), 0); 42. } // cutLine 43. 44.void printLine(Line const& line) { 45. cout << line.data << endl; 46.} 47. 48.void readLines(vector<Line>& lines, char delim) { 49. Line line; 50. while (getline(cin, line.data)) { 51. if (0 != delim) 52. cutLine(line, delim); 53. else { 54. line.first = line.data; 55. line.second = 0; 56. line.third = 0; 57. } 58. lines.push_back(line); 59. } 60.} // readLines 61. 62.bool firstOrder(Line const& l1, Line const& l2) { 63. return l1.first < l2.first; 64.} 65. 66.bool secondOrder(Line const& l1, Line const& l2) { 67. return l1.second < l2.second; 68.} 69. 70.bool thirdOrder(Line const& l1, Line const& l2) { 71. return l1.third < l2.third; 72.} 73. 74.void parseArgs(int argc, char const * argv[], char& delim, int& key, bool& reverse) { 75. for (int index = 1; index < argc; ++index) { 76. string const arg = argv[index]; 77. if ("-t" == arg && index + 1 < argc) { 78. string const delimStr = argv[index + 1]; 79. if (1 == delimStr.size()) { 80. delim = delimStr[0]; 81. ++index; 82. continue; 83. } 84. } 85. if ("-k" == arg && index + 1 < argc) { 86. string const keyStr = argv[index + 1]; 87. int keyInt = strtol(argv[index + 1], 0, 0); 88. if (1 == keyStr.size() && keyInt >= 1 && keyInt <= 3) { 89. key = keyInt; 90. ++index; 91. continue; 92. } 93. } 94. if ("-r" == arg) 95. reverse = true; 96. } 97. if (0 == delim) 98. key = 1; 99.} // parseArgs 100. 101.int main(int argc, char const * argv[]) { 102. char delim = 0; 103. int key = 1; 104. bool reverse = false; 105. parseArgs(argc, argv, delim, key, reverse); 106. vector<Line> lines; 107. readLines(lines, delim); 108. if (2 == key) 109. std::sort(lines.begin(), lines.end(), secondOrder); 110. else if (3 == key) 111. std::sort(lines.begin(), lines.end(), thirdOrder); 112. else 113. std::sort(lines.begin(), lines.end(), firstOrder); 114. if (reverse) 115. std::for_each(lines.rbegin(), lines.rend(), printLine); 116. else 117. std::for_each(lines.begin(), lines.end(), printLine); 118. return 0; 119.}
Correction by tdd

