输入迭代器
- 能构造和默认构造
- 能复制或赋值(迭代器)
- 能比较相等
- 能向前移动
- 能读取值(read-only)
- 代码
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
cout<<"input:";
istream_iterator<int>a(cin);
istream_iterator<int>b;
while (1) {
// Error:read-only
// *a = 3;
cout<<*a<<endl;
a++;
if (a == b) {
break;
}
}
}
输出迭代器
- 构造和默认构造
- 能复制或赋值(迭代器)
- 能比较相等
- 能向前移动
- 能写入值(write-only)
- 代码
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
cout<<"input:";
ostream_iterator<int>myout(cout,"\n");
for (int i = 0; i<5; i++) {
*myout = i;
myout++;
}
return 0;
}
本文介绍了C++标准库中的输入迭代器和输出迭代器的基本概念及使用方法。通过示例代码展示了如何利用输入迭代器从标准输入读取整数序列,并通过输出迭代器将整数序列写入到标准输出中。深入理解这两种迭代器有助于提高C++程序的数据处理能力。
259

被折叠的 条评论
为什么被折叠?



