C++ get each digit in int

前端 未结 13 1288
谎友^
谎友^ 2020-12-02 21:50

I have an integer:

int iNums = 12476;

And now I want to get each digit from iNums as integer. Something like:

foreach(iNum          


        
相关标签:
13条回答
  • 2020-12-02 22:03

    Here is a more generic though recursive solution that yields a vector of digits:

    void collect_digits(std::vector<int>& digits, unsigned long num) {
        if (num > 9) {
            collect_digits(digits, num / 10);
        }
        digits.push_back(num % 10);
    }
    

    Being that there are is a relatively small number of digits, the recursion is neatly bounded.

    0 讨论(0)
  • 2020-12-02 22:04

    I know this is an old post, but all of these answers were unacceptable to me, so I wrote my own!

    My purpose was for rendering a number to a screen, hence the function names.

    void RenderNumber(int to_print)
    {
        if (to_print < 0)
        {
            RenderMinusSign()
            RenderNumber(-to_print);
        }
        else
        {
            int digits = 1; // Assume if 0 is entered we want to print 0 (i.e. minimum of 1 digit)
            int max = 10;
    
            while (to_print >= max) // find how many digits the number is 
            {
                max *= 10;
                digits ++;
            }
    
            for (int i = 0; i < digits; i++) // loop through each digit
            {
                max /= 10;
                int num = to_print / max; // isolate first digit
                to_print -= num * max; // subtract first digit from number
                RenderDigit(num);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-02 22:05

    The answer I've used is this simple function:

    int getDigit(int n, int position) {
    
        return (n%(int)pow(10, position) - (n % (int)pow(10, position-1))) / (int)pow(10, position-1);
    
    }
    

    Hope someone finds this helpful!

    0 讨论(0)
  • 2020-12-02 22:10

    I don't know if this is faster or slower or worthless, but this would be an alternative:

    int iNums = 12476;
    string numString;
    stringstream ss;
    ss << iNums;
    numString = ss.str();
    for (int i = 0; i < numString.length(); i++) {
        int myInt = static_cast<int>(numString[i] - '0'); // '0' = 48
        printf("%i-", myInt);
    }
    

    I point this out as iNums alludes to possibly being user input, and if the user input was a string in the first place you wouldn't need to go through the hassle of converting the int to a string.

    (to_string could be used in c++11)

    0 讨论(0)
  • 2020-12-02 22:13

    Drawn from D.Shawley's answer, can go a bit further to completely answer by outputing the result:

    void stream_digits(std::ostream& output, int num, const std::string& delimiter = "")
    {
        if (num) {
            stream_digits(output, num/10, delimiter);
            output << static_cast<char>('0' + (num % 10)) << delimiter;
        }
    }
    
    void splitDigits()
    {
        int num = 12476;    
        stream_digits(std::cout, num, "-");    
        std::cout << std::endl;
    }
    
    0 讨论(0)
  • 2020-12-02 22:21

    Based on @Abyx's answer, but uses div so that only 1 division is done per digit.

    #include <cstdlib>
    #include <iostream>
    
    void print_each_digit(int x)
    {
        div_t q = div(x, 10);
    
        if (q.quot)
            print_each_digit(q.quot);
    
        std::cout << q.rem << '-';
    }
    
    int main()
    {
        print_each_digit(12476);
        std::cout << std::endl;
        return 0;
    }
    

    Output:

    1-2-4-7-6-
    

    N.B. Only works for non-negative ints.

    0 讨论(0)
提交回复
热议问题