直接上代码:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cctype>
// Function to compare two strings in a natural way
bool naturalCompare(const std::string& a, const std::string& b) {
size_t i = 0, j = 0;
while (i < a.size() && j < b.size()) {
// Skip leading zeros in numbers
if (std::isdigit(a[i]) && std::isdigit(b[j])){
size_t valA = 0, valB = 0;
std::string strA {};
std::string strB {};
while(i < a.length() && std::isdigit(a[i])) {
valA += (a[i] -'0');
if(valA != 0) {
strA.push_back(a[i]);
}
i++;
}
while(j < b.length() && std::isdigit(b[j])) {
valB += (b[j] -'0');
if(valB != 0) {
strB.push_back(b[j]);
}
j++;
}
if(strA != strB && strA.size() == strB.size()