Re: [cpp] 关于STL中string的问题
Chen Shuo
giantchen at gmail.com
Fri Mar 3 09:18:03 CST 2006
你觉得优就用呗。
On 3/2/06, Wu Yongwei <adah at sh163.net> wrote:
> 苏益ꪏ wrote:
>
> > 请先看这段代码。
> > ----------------------------------------------------------------
> > //我希望把string赋值10,然后显示
> > string Buffer;
> > sprintf( Buffer.begin(),"%d",10);
> > MessageBox(NULL,Buffer.begin(),NULL,MB_OK);
> > ----------------------------------------------------------------
> >
> > 建个程序,编译它,是通不过的。原因是Buffer没有初值,调用Buffer.begin()返
> > 回是NULL,第三句用MessageBox弹出一个NULL的字符串,这是不可取的。
> >
> > 所以我改正以后,程序为
> > ---------------------------------------------------------------
> > //改正后的程序
> > string Buffer = "a"; //我随便给了个初值
> > sprintf( Buffer.begin(),"%d",10);
> > MessageBox(NULL,Buffer.begin(),NULL,MB_OK);
> > ----------------------------------------------------------------
>
> C++里的正规做法是使用stringstream(#include <sstream>):
>
> std::ostringstream oss;
> oss << 10;
> MessageBox(NULL, oss.str().c_str(), NULL, MB_OK);
>
> 呵呵,Chen Shuo,不好意思,我觉得这又是一个显示iostreams优于printf的例子。
>
> 吴咏炜
> _______________________________________________
> Cpp mailing list
> Cpp at codingnow.com
> http://codingnow.com/mailman/listinfo/cpp
>
More information about the Cpp
mailing list