linux int to string 方法

最近从windows 移植程序的时候发现to_string()函数在linux 中不能用,网上找了几种方法,觉得使用stringstream对象来实现类型转化比较好一点。

你只需在你工程中加入下面的to_sting()函数,就不需要修改你原程序了。(这篇只是本人移植工程时的心得,高手绕过!)

/*

 * to_string.cpp

 *  Created on: 2014年9月11日

 * Author: tursunjan

 *      linux int to string

 */

#include<iostream>

#include<sstream>

using namespace std;

string to_string(int a)

{

   ostringstream ostr;

   ostr << a;

   string astr = ostr.str();

   //cout << astr <<endl;

   return astr ;

}

int main()

{

    int testNum=10;

    cout<<to_string(testNum)<<endl;

    return 0;

}

 





你可能感兴趣的:(String)