linux int to string 方法

时间:2014-11-11 02:01:23   收藏:0   阅读:213

最近从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;
}

 





评论(0)
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!