牵着老婆满街逛

严以律己,宽以待人. 三思而后行.
GMail/GTalk: yanglinbo#google.com;
MSN/Email: tx7do#yahoo.com.cn;
QQ: 3 0 3 3 9 6 9 2 0 .

std::string 的 fprintf()

#include <string>
#include 
<iostream>
#include 
<sstream>
using namespace std;

int main ()
{
    stringstream sstr;
    
string str;
    
const char*  A = "ddddddddddddddd";
    
double d = 0.001504f;
    sstr 
<< A << 112345 << d;
    str 
= sstr.str();
    cout
<<str.c_str()<<endl;
    
return 0;
}

char[]可以用fprintf实现,CString可以用format实现此需求.
CString是MFC的类,char[]又害怕溢出,
string是最好的选择,而且它还有stringstream进行流操作,
用起来就像cout一样.

清除缓存要用
sstr.str("");
我最初用的
sstr.clear();
但是结果是,不奏效....缓存依然存在.
后来查资料,str("")才可以,而clear()仅仅是清除掉了flag罢了,至于是什么flag,只有在查询官方手册才知道了.....

posted on 2007-07-21 19:20 杨粼波 阅读(578) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理