STL输入迭代器和输出迭代器

来源:互联网 发布:从新网代理商转出域名 编辑:程序博客网 时间:2024/06/09 18:56

输入迭代器

  • 能构造和默认构造
  • 能复制或赋值(迭代器)
  • 能比较相等
  • 能向前移动
  • 能读取值(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;}