How do I split an int into its digits?

前端 未结 15 1508
渐次进展
渐次进展 2020-11-27 17:51

How can I split an int in c++ to its single numbers? For example, I\'d like to split 23 to 2 and 3.

15条回答
  •  失恋的感觉
    2020-11-27 17:55

    I don't necessarily recommend this (it's more efficient to work with the number rather than converting it to a string), but it's easy and it works :)

    #include 
    #include 
    #include 
    #include 
    
    #include 
    
    int main()
    {
        int n = 23984;
        std::string s = boost::lexical_cast(n);
        std::copy(s.begin(), s.end(), std::ostream_iterator(std::cout, "\n"));
        return 0;
    }
    

提交回复
热议问题