int转string

来源:互联网 发布:wod数据海洋 编辑:程序博客网 时间:2024/05/20 00:39
#include <sstream>
/*
convert other data to string
usage :
    string str = m_toStr<int>(12345);
*/
template <class T> string m_toStr(T tmp)
{
    stringstream ss;
    ss << tmp;
    return ss.str();

}


c++11可以这样:

#include<string>
#include<iostream>
using namespace std;
 
int main()
{
    int i = 42;
    string s = to_string(i);
    cout << s << endl;
 
    string sint = "56";
    int n = stoi(sint);
    cout << n << endl;
 
    return 0;
}

0 0