Easiest way to convert int to string in C++

后端 未结 28 1755
甜味超标
甜味超标 2020-11-21 06:42

What is the easiest way to convert from int to equivalent string in C++. I am aware of two methods. Is there any easier way?

(1)



        
28条回答
  •  再見小時候
    2020-11-21 07:18

    string number_to_string(int x) {
    
        if (!x)
            return "0";
    
        string s, s2;
        while(x) {
            s.push_back(x%10 + '0');
            x /= 10;
        }
        reverse(s.begin(), s.end());
        return s;
    }
    

提交回复
热议问题