How do I convert a long to a string in C++?

后端 未结 12 1285
慢半拍i
慢半拍i 2020-12-09 07:12

How do I convert a long to a string in C++?

12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-09 07:44

    You could use stringstream.

    #include 
    
    // ...
    std::string number;
    std::stringstream strstream;
    strstream << 1L;
    strstream >> number;
    

    There is usually some proprietary C functions in the standard library for your compiler that does it too. I prefer the more "portable" variants though.

    The C way to do it would be with sprintf, but that is not very secure. In some libraries there is new versions like sprintf_s which protects against buffer overruns.

提交回复
热议问题