zoukankan      html  css  js  c++  java
  • int转string的3种方法

    现在都用宽字节(unicode)了,所以建议使用wstring取代string(ansi版)

    不像CString一样有Format函数去转换,string/wsting类型需要手动转换

    #include <strsafe.h>
    #include <sstream>
    using namespace std;
    using std::endl;
    using std::wcout;
    using std::wstring;
    
    wstring int2string()
    {
        wstring strNum;
        int nNum = 1039;
        TCHAR szNum[16] = { 0 };
        std::wcout.imbue(std::locale("chinese"));            //让wout可以输出中文;
    
        //方法1 _itoa_s/_itow_s法
        _itow_s(nNum, szNum, 10);
        strNum = szNum;
        wcout <<L"测试语句,方法1: " <<strNum.c_str()<< endl;
    
    
        //方法2 StringCchPrintf/sprintf法
        strNum.clear();
        StringCchPrintf(szNum, 16, _T("%d"), nNum);
        strNum = szNum;
        wcout << L"测试语句,方法2: " << strNum.c_str() << endl;
    
        //方法3 stringstream/wstringstream法
        strNum.clear();
        wstringstream strNum2;
        strNum2 << nNum;
        strNum2 >> strNum;
        wcout << L"测试语句,方法3: " << strNum.c_str() << endl;
    
        return strNum;
    }
  • 相关阅读:
    woj 1574
    UESTC 594 我要长高 dp单调队列
    HDU 3401 Trade dp 单调队列优化
    HDU 2844 Coins 多重背包
    2-1
    1-2
    1-1
    12-1
    9-1
    14-8
  • 原文地址:https://www.cnblogs.com/Lthis/p/4199162.html
Copyright © 2011-2022 走看看