fork download
  1. #include <vector>
  2. #include <string>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <iterator>
  6. #include <cstring>
  7. using namespace std;
  8.  
  9. string remove_dot(const char *p)
  10. {
  11. const char *dot = strchr(p, '.');
  12. return dot ? string(p, dot - p) : string(p);
  13. }
  14.  
  15. bool compare_filenames(string a, string b)
  16. {
  17. char *pA, *pB;
  18. long A = strtol(a.c_str(), &pA, 10),
  19. B = strtol(b.c_str(), &pB, 10);
  20. if (A < B)
  21. return true;
  22. if (A == B)
  23. return remove_dot(pA) < remove_dot(pB);
  24. return false;
  25. }
  26.  
  27. int main(int, char **)
  28. {
  29. const char *v[] ={
  30. "a10",
  31. "10a",
  32. "1ab",
  33. "2a",
  34. "1aa",
  35. "a2",
  36. "1bb",
  37. "1a",
  38. "a1",
  39. "1b",
  40. "10b",
  41. "2b",
  42. "b1",
  43. "b10"
  44. };
  45. vector<string> t(v, v+(sizeof(v)/sizeof(char*)));
  46. sort(t.begin(), t.end(), compare_filenames);
  47. copy(t.begin(), t.end(), ostream_iterator<string>(cout, "\n"));
  48. return 0;
  49. }
Success #stdin #stdout 0.02s 2864KB
stdin
Standard input is empty
stdout
a1
a10
a2
b1
b10
1a
1aa
1ab
1b
1bb
2a
2b
10a
10b