关于流的问题

kaphoon 2004-02-13 11:07:28
问题一:
#include<iostream>
#include<vector>
#include<cstdlib>
using namespace std;
int main()
{
vector<int> v;
copy(istreambuf_iterator<int>(cin),istreambuf_iterator<int>(),back_inserter(v));
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
system("pause");
}
为什么不能运行,说istreambuf_iterator没有声明?

问题二:
#include<iostream>
#include<functional>
#include<algorithm>
#include<cstdlib>
using namespace std;
template<class T>
struct print: public unary_function<T,void>
{
print(ostream& out) :os(out),count(0){}
void operator()(T x){os<<x<<' ';++count;}
ostream& os;
int count;
};

int main()
{
int A[] = {1,4,2,8,6,7};
const int N = sizeof(A)/sizeof(int);
print<int> p =for_each(A,A+N,print<int>(cout));
cout<<endl<<p.count<<"objects printed."<<endl;
system("pause");
}

/*为什么在print的成员变量os是ostream&,构造函数的变量也是这种形式呢?
声明引用变量时,不是要进行初始化的吗?
如果去了&,将不能编译,
ios& operator=(ios&); Not allowed!
ios (const ios&); Not allowed!
我在什么地方用了呢?
*/
...全文
27 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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