std::oststream VS std::ostringstream

来源:互联网 发布:wolf at the door 知乎 编辑:程序博客网 时间:2024/04/30 04:14

用如下程序段在for循环中分别测试std::oststream  和 std::ostringstream,可以发现std::oststream  有内存泄露,最好使用std::ostringstream。

  std::ostringstream oss, oss2;
   std::string strVal ; double var = 200.03234;
   //读入三位小数
   oss.setf(ios::fixed, ios::floatfield);
   oss.precision(3);
   oss << (double)var << '/0';
   
   double dblVal = 0;
   std::istringstream iss(oss.str());
   iss >> dblVal;
   
   //去掉尾数的0
   oss2<<std::setprecision(15)
    << dblVal << '/0';
   strVal = oss2.str().c_str();

原创粉丝点击