在使用C++的过程中,发现std:string是个很好用的东东,不过感觉不能象sprintf( "%d“, 123)或者CString.Format()的形式进行字符串的格式化,感觉不是十分舒服,经过寻找,发现以下方法可以进行字符串的格式化:
1
#include <sstream>
2
#include <iostream>
3
4
using namespace std;
5
6
ostringstream ostr;
7
ostr << "d = " << 123 << "f = " << 12.345 << "test format" << std:endl;
8
string str = ostr.str();
9
cout << ostr.str().c_str();

2

3

4

5

6

7

8

9

使用这样的方法,就能够使用std:string的格式化了,让std:string变得更好用。