c++ 自然排序-window文件排序
转载:(10条消息) C++字符串操作之字符串自然排序_zhanghm1995的博客-CSDN博客
//自然排序 bool compareNat(const std::string &a, const std::string &b) { if (a.empty()) return true; if (b.empty()) return false; if (std::isdigit(a[0]) && !std::isdigit(b[0])) return true; if (!std::isdigit(a[0]) && std::isdigit(b[0])) return false; if (!std::isdigit(a[0]) && !std::isdigit(b[0])) { if (std::toupper(a[0]) == std::toupper(b[0])) return compareNat(a.substr(1), b.substr(1)); return (std::toupper(a[0]) < std::toupper(b[0])); } // Both strings begin with digit --> parse both numbers std::istringstream issa(a); std::istringstream issb(b); int ia, ib; issa >> ia; issb >> ib; if (ia != ib) return ia < ib; // Numbers are the same --> remove numbers and recurse std::string anew, bnew; std::getline(issa, anew); std::getline(issb, bnew); return (compareNat(anew, bnew)); } bool judge(const pair<string, string> a, const pair<string, string> b) { return compareNat(a.first, b.first); }