std::string formatting like sprintf

前端 未结 30 2614
野趣味
野趣味 2020-11-22 04:42

I have to format std::string with sprintf and send it into file stream. How can I do this?

30条回答
  •  -上瘾入骨i
    2020-11-22 05:09

    boost::format() provides the functionality you want:

    As from the Boost format libraries synopsis:

    A format object is constructed from a format-string, and is then given arguments through repeated calls to operator%. Each of those arguments are then converted to strings, who are in turn combined into one string, according to the format-string.

    #include 
    
    cout << boost::format("writing %1%,  x=%2% : %3%-th try") % "toto" % 40.23 % 50; 
    // prints "writing toto,  x=40.230 : 50-th try"
    

提交回复
热议问题