【问题标题】:iostream use of << to construct stringiostream 使用 << 构造字符串
【发布时间】:2010-01-30 15:29:20
【问题描述】:

int iCount;
char szB[128];
sprintf (szB,"%03i", iCount);

【问题讨论】:

标签: c++ iostream


【解决方案1】:
using namespace std;    
stringstream ss;
ss << setw(3) << setfill('0') << iCount;
string szB = ss.str();

【讨论】:

    【解决方案2】:
    #include <iostream>
    #include <sstream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    int main() {
        int iCount = 42;
        ostringstream buf;
        buf << setw(3) << setfill('0') << iCount;
        string s = buf.str();
        cout << s;
    }
    

    【讨论】:

      【解决方案3】:

      这没有任何意义。

      如果您想做类似的事情,请在 C++ 中使用 std::ostringstream

       std::ostringstream s;
       int x=<some_value>;
       s<< std::setw(3) << std::setfill('0') <<x;
       std::string k=s.str();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-05
        • 2020-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-19
        • 1970-01-01
        相关资源
        最近更新 更多