C++ 实现任意基本类型转换为 string 类型
可以使用模板和strstream
的流操作来实现int
、char
等基本类型转换string
的功能。
代码如下:
#include <strstream>
using namespace std;
// any simple type convert to string
template<class src_type>
string type2str(src_type src) {
strstream ss;
ss << src;
string ret;
ss >> ret;
return ret;
}