std::string formatting like sprintf

前端 未结 30 2601
野趣味
野趣味 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条回答
  •  花落未央
    2020-11-22 05:29

    You can format C++ output in cout using iomanip header file. Make sure that you include iomanip header file before you use any of the helper functions like setprecision, setfill etc.

    Here is a code snippet I have used in the past to print the average waiting time in the vector, which I have "accumulated".

    #include
    #include
    #include
    #include
    
    ...
    
    cout<< "Average waiting times for tasks is " << setprecision(4) << accumulate(all(waitingTimes), 0)/double(waitingTimes.size()) ;
    cout << " and " << Q.size() << " tasks remaining" << endl;
    

    Here is a brief description of how we can format C++ streams. http://www.cprogramming.com/tutorial/iomanip.html

提交回复
热议问题