 
 
 
 
 
 
 
  
数値を文字列に変換するには、C では sprintf() を用いるのが便利だ が、C++ では ostrstream クラスを利用する。
| test-strstream1.C -- これは反則? | 
| #include <iostream.h>
#include <strstream.h>
int main()
{
  int N;
  double x, err;
  ostrstream ost;
  N = 123; x = 1.234; err = 1.2e-34;
  ost.form("N=%2d,x=%f,err=%e", N, x, err);
  cout << ost.str() << endl;
}
 | 
| test-strstream2.C | 
| // test-strstream1.C
#include <iostream.h>
#include <iomanip.h>
#include <strstream.h>
int main()
{
  int N;
  double x, err;
  ostrstream ost;
  N = 123; x = 1.234; err = 1.2e-34;
  ost << "N=" << setw(2) << N << ",x=" << setiosflags(ios::fixed) << x
      << ",err=" << setiosflags(ios::scientific) << err;
  cout << ost.str() << endl;
}
 | 
| test-strstream.out | 
| mathpc00% ./test-strstream1 N=123,x=1.234000,err=1.200000e-34 mathpc00% ./test-strstream2 N=123,x=1.234000,err=1.2e-34 mathpc00% | 
 
 
 
 
 
 
