std::string formatting like sprintf

前端 未结 30 3128
野趣味
野趣味 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
    闹比i (楼主)
    2020-11-22 05:17

    string doesn't have what you need, but std::stringstream does. Use a stringstream to create the string and then extract the string. Here is a comprehensive list on the things you can do. For example:

    cout.setprecision(10); //stringstream is a stream like cout
    

    will give you 10 decimal places of precision when printing a double or float.

提交回复
热议问题