template<typename T>
void PrettyFormat(T t1,string& s)
{
    ostringstream temp;
    temp/*<< setw(4)*/ <<t1;
    s =temp.str();
}
记得要加入
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <sstream>

其实用boost的lexcal_cast也可以,但那个慢一些。
有些地方提到了 sprintf, snprintf ,前一个不安全, 后一个不是标准。
strstream 也不是标准,所以还是这个好些。

ps:单独用(不用模版)那么每次使用完ostringstream后要 clean(),否则会有意外的结果。

相关文章:

  • 2021-10-18
  • 2021-07-28
  • 2022-12-23
  • 2022-12-23
  • 2021-12-19
  • 2021-09-15
  • 2021-10-10
  • 2022-01-09
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-13
  • 2021-12-05
  • 2022-12-23
  • 2021-07-24
相关资源
相似解决方案