You can use boost::regex
to extract a number as a string, convert the string to a number in a custom comparator for std::sort
, to sort the std::vector
your files
boost::regex re("(\\d+)"); boost::match_results<std::string::const_iterator> what1,what2; template <typename T> T st2num ( const std::string &Text ) { std::stringstream ss(Text); T result; return ss >> result ? result : 0; } struct mysort { bool operator ()(const std::string & a,const std::string & b) { boost::regex_search(a.cbegin(), a.cend(), what1, re, boost::match_default); boost::regex_search(b.cbegin(), b.cend(), what2, re, boost::match_default); return st2num<int>(what1[1]) < st2num<int>(what2[1]); } };
And then,
//std::vector<std::string> vec{"_X1.bla.txt", "_X101.bla.txt", "_X47.bla.txt"};
std::sort( vec.begin() , vec.end() ,mysort() );
P0w source share