fork download
  1.  
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <cctype>
  6. #include <string>
  7.  
  8. struct Comp{
  9.  
  10. auto get_num (const std::string& a)
  11. {
  12. auto it1 = std::find_if( a.begin(), a.end(), ::isdigit );
  13. auto it2 = std::find_if( a.begin(), a.end(), [](char x){ return x == '.' ;}) ;
  14. /* Do some checks here for std::string::npos*/
  15. auto pos1 = std::distance( a.begin(), it1) ;
  16. auto pos2 = std::distance( it1, it2) ;
  17. return std::stoi (a.substr( pos1, pos2 )) ;
  18. }
  19.  
  20. bool operator () (const std::string& a, const std::string& b)
  21. {
  22. return get_num (a) < get_num (b) ;
  23. }
  24.  
  25. };
  26.  
  27.  
  28. int main ()
  29. {
  30.  
  31. std::vector <std::string> vec {
  32. "some_name_10.xyz",
  33. "some_name_1.xyz",
  34. "some_name_2.xyz",
  35. "some_name_12.xyz",
  36. "some_name_3.xyz"
  37. };
  38.  
  39. std::sort( vec.begin() , vec.end(), Comp());
  40.  
  41. for(auto x: vec)
  42. std::cout << x << std::endl;
  43.  
  44. }
Success #stdin #stdout 0s 3452KB
stdin
Standard input is empty
stdout
some_name_1.xyz
some_name_2.xyz
some_name_3.xyz
some_name_10.xyz
some_name_12.xyz