How to get the digits of a number without converting it to a string/ char array?

前端 未结 15 1317
既然无缘
既然无缘 2020-11-27 04:07

How do I get what the digits of a number are in C++ without converting it to strings or character arrays?

15条回答
  •  长情又很酷
    2020-11-27 04:52

    Since everybody is chiming in without knowing the question.
    Here is my attempt at futility:

    #include 
    
    template int getDigit(int val)       {return getDigit(val/10);}
    template<>      int getDigit<1>(int val)    {return val % 10;}
    
    int main()
    {
        std::cout << getDigit<5>(1234567) << "\n";
    }
    

提交回复
热议问题