I want to split an integer of type unsigned long long
to its digits.
Any comments and suggestions are always welcome.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<unsigned short> IntegerDigits(unsigned long long input)
{
vector<unsigned short> output;
const unsigned short n = log10(ULLONG_MAX);
unsigned long long divisor = pow(10, n);
bool leadingZero = true;
for (int i = 0; i < n + 1; i++)
{
unsigned short digit = input / divisor % 10;
if (!leadingZero || digit != 0)
{
output.push_back(digit);
leadingZero = false;
}
divisor /= 10;
}
return output;
}
void main()
{
vector<unsigned short> output = IntegerDigits(ULLONG_MAX);
cout << ULLONG_MAX << ": [";
for (auto y : output)
cout << y << ", ";
cout << "\b\b]";
cout << ""<<endl;
}
short n = log10(ULLONG_MAX)
will this be rounded up or down? Why? \$\endgroup\$ – greybeard Oct 31 '20 at 6:33log10(ULLONG_MAX) + 1
represents "the max number of digits" forunsigned long long
. \$\endgroup\$ – Money Oriented Programmer Oct 31 '20 at 6:40ULLONG_MAX
was 2⁶⁶ (2³³×2³³)? \$\endgroup\$ – greybeard Oct 31 '20 at 6:44pow(10, n)
may not be "losslessly" assignable to anunsigned long long
. \$\endgroup\$ – greybeard Oct 31 '20 at 7:54