cout<

blazing216 2009-02-18 08:49:31
代码是这样的

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

main()
{
ostringstream os("This is a test.");
cout << os.rdbuf();//改成cout << os.str()可以,但为什么rdbuf()不行?
system("pause");
}

《Thinking in C++》2nd里有类似cout << os.rdbuf()的程序,我试过,和书中所说的结果不一样。

PS:我用的是dev-cpp
...全文
220 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
biweilun 2009-02-19
  • 打赏
  • 举报
回复
ostringstream是输出字符流,istringstream是输入字符流,LZ搞错了
chenyingshu 2009-02-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 feng4206yu 的回复:]
C/C++ code
#include<iostream>
#include<sstream>
using namespace std;

main()
{
istringstream os("This is a test.");//
cout << os.rdbuf();
system("pause");
}
[/Quote]
看出里面的区别了吗?
feng4206yu 2009-02-19
  • 打赏
  • 举报
回复

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

main()
{
istringstream os("This is a test.");//
cout << os.rdbuf();
system("pause");
}
blazing216 2009-02-19
  • 打赏
  • 举报
回复
我改了下代码

#include<sstream>
#include<iostream>

int main()
{
ostringstream os("This is a test.",ios::in);
cout << os.rdbuf();
system("pause");
}

在os的定义中加了ios::in,就可以显示出来了。为什么是这样?
还有当我把定义改为istringstream is("This is a test.",ios::out);依然可以显示出来,并没有因为用ios::out打开而受到影响,为什么?
UndefinedCoder 2009-02-19
  • 打赏
  • 举报
回复
学些了
blazing216 2009-02-19
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 fhtingtian 的回复:]
os里就没内容,按照ls几位的修改就行了
[/Quote]
没有内容怎么理解,os里不是有"This is a test."吗?用os.str()就可以把内容取出来。os.rdbuf()返回指向os的指针, 而且<<也有ostream& operator<< (streambuf* sb )这种重载方式,为什么显示不出"This is a test."?istringstream类的rdbuf()和ostringstream类的rdbuf()返回的streambuf*有什么不同吗?
tangshuiling 2009-02-19
  • 打赏
  • 举报
回复

cout读取的是输入流,输入流要有东西才可以输出东东!
bfhtian 2009-02-19
  • 打赏
  • 举报
回复
os里就没内容,按照ls几位的修改就行了

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧