C++中 int、string等类型转换方法

    xiaoxiao2021-03-25  143

    1、int 和 string 互相转换

    (1)int ==> string

    string int2str(const int i) { stringstream ss; ss << i; return ss.str(); }

    (2)string ==> int

    int str2int(const string s) { stringstream ss(s); int temp; ss >> temp; return temp; }

    //以上转换需要包含  #include <sstream>

    //在C++中推荐使用流来进行类型转换。

    //同样的道理,string 和 float 的转换只需要将上面的int 改为 float即可。

    转载请注明原文地址: https://ju.6miu.com/read-3816.html

    最新回复(0)