[Solved] how to convert a string of number with trailing x’s into a list of unsigned numbers


Create two variables:

std::string maxVal = fn; 
std::replace(maxVal, 'X', '9'); 
std::string minVal = fn;
std::replace(minVal, 'X', '0');

Now you can loop with

for (auto i = std::stoi(minVal), j = std::stoi(maxVal); i <= j; ++i) {
    codes.push_back(i);
}

The whole Code

#include <algorithm>
#include <iostream>
#include <list>

std::list<unsigned> stringToCode(std::string fn) {
   std::string maxVal = fn; 
   std::replace(std::begin(maxVal), std::end(maxVal), 'X', '9'); 
   std::string minVal = fn;
   std::replace(std::begin(minVal), std::end(minVal), 'X', '0');
   std::list<unsigned> codes;
   for (auto i = std::stoi(minVal), j = std::stoi(maxVal); i <= j; ++i) {
       codes.push_back(i);
   }

   return codes;
}

int main() {
  std::string number = "4XXX";

  std::list<unsigned> codes = stringToCode(number);

   for (const auto code : codes) {
      std::cout << code << std::endl;
   }

   return 0;
}

solved how to convert a string of number with trailing x’s into a list of unsigned numbers