如何将带有尾随 x 的数字字符串转换为无符号数字列表
how to convert a string of number with trailing x's into a list of unsigned numbers
我需要将 4 个字符的字符串转换为无符号数字列表
例如 222X = [2220, 2221, 2222, 2223, 2224, 2225, 2226, 2227, 2228, 2229 ]
类似地,我应该能够转换 22XX(100 个数字)、2XXX(应该产生 1000 个)数字。
有什么快速的方法吗
我有以下解决方案,但不是很干净..
std::list<unsigned> stringToCode(std::string fn)
{
std::list<unsigned> codes;
unsigned count = std::count(fn.begin(), fn.end(), 'X');
unsigned none_x = std::stoi(fn);
unsigned numbers_to_generate = std::pow(10, count);
unsigned overrule = none_x * numbers_to_generate;
for (int i = 0; i < numbers_to_generate; i++) {
unsigned fnumber = none_x * std::pow(10, count) + i;
codes.push_back(fnumber);
}
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;
}
创建两个变量:
std::string maxVal = fn;
std::replace(maxVal, 'X', '9');
std::string minVal = fn;
std::replace(minVal, 'X', '0');
现在你可以循环使用
for (auto i = std::stoi(minVal), j = std::stoi(maxVal); i <= j; ++i) {
codes.push_back(i);
}
整个代码
#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;
}
我需要将 4 个字符的字符串转换为无符号数字列表 例如 222X = [2220, 2221, 2222, 2223, 2224, 2225, 2226, 2227, 2228, 2229 ] 类似地,我应该能够转换 22XX(100 个数字)、2XXX(应该产生 1000 个)数字。 有什么快速的方法吗
我有以下解决方案,但不是很干净..
std::list<unsigned> stringToCode(std::string fn)
{
std::list<unsigned> codes;
unsigned count = std::count(fn.begin(), fn.end(), 'X');
unsigned none_x = std::stoi(fn);
unsigned numbers_to_generate = std::pow(10, count);
unsigned overrule = none_x * numbers_to_generate;
for (int i = 0; i < numbers_to_generate; i++) {
unsigned fnumber = none_x * std::pow(10, count) + i;
codes.push_back(fnumber);
}
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;
}
创建两个变量:
std::string maxVal = fn;
std::replace(maxVal, 'X', '9');
std::string minVal = fn;
std::replace(minVal, 'X', '0');
现在你可以循环使用
for (auto i = std::stoi(minVal), j = std::stoi(maxVal); i <= j; ++i) {
codes.push_back(i);
}
整个代码
#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;
}