Easiest way to convert int to string in C++

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

    First include:

    #include 
    #include 
    

    Second add the method:

    template 
    string NumberToString(T pNumber)
    {
     ostringstream oOStrStream;
     oOStrStream << pNumber;
     return oOStrStream.str();
    }
    

    Use the method like this:

    NumberToString(69);
    

    or

    int x = 69;
    string vStr = NumberToString(x) + " Hello word!."
    

提交回复
热议问题