Easiest way to convert int to string in C++

后端 未结 28 2358
甜味超标
甜味超标 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:22

    Here's another easy way to do

    char str[100];
    sprintf(str, "%d", 101);
    string s = str;
    

    sprintf is a well-known one to insert any data into a string of the required format.

    You can convert a char * array to a string as shown in the third line.

提交回复
热议问题