Lecture Notes for SE 598, Data Structures and Algorithms

5 July 2000 - Iterators, Part 2


  1. stream iterators

    1. consider
      list<char> l;
      char c;
      while ((cin >> c) != EOF)
        l.push_back(c);
      

    2. using stream iterators, this can be replaced by
      list<char> l;
      istream_iterator<char> i(cin);
      while (i != istream_iterator<char>())
        l.push_back(*i++))
      

    3. input-stream iterators

      1. istream_iterator<t>(istream &) - return an iterator

      2. istream_iterator<t>() - return the end iterator

      3. l-value * - read the next value

      4. pre- and post-fix ++ - go to the next value

      5. == - iterator equality, but oddly

      6. type t must have operator >> defined

    4. output-stream iterators

      1. ostream_iterator<t>(istream &) - return an iterator

      2. ostream_iterator<t>(istream &, const char *) - return an iterator

      3. ostream_iterator<t>() - return the end iterator

      4. r-value * - write the next value

      5. pre- and post-fix *(++) - does nothing

      6. type t must have operator << defined

    5. keep in mind the stream iterators are not iterators - different types

      1. vector<char> strs(istream_iterator<char>(cin), istream_iterator<char>()) is illegal

  2. iterators and generic algorithm parameters

    1. different iterator kinds have different capabilities

    2. generic algorithms specify the weakest, least capable, iterator kind

      1. least capable iterators are the least restrictive

    3. but, there's only one iterator type

      1. still get compile time errors due to missing operators

  3. constant vs. mutable iterators

    1. iterators of type const_iterator

    2. l-value * operators don't apply to const iterators - no writing

      1. *i = v is a compile time error if i is a const iterator

    3. where possible use and specify constant iterators

      1. more information for code readers and compilers

      2. the program is more durable

    4. iterators are implicitly promoted to const iterators

      1. list<t>::iterator li = l.begin();
        list<t>::const_iterator cli = li;
        

    5. constant containers produce constant iterators

      1. const list<t> l(s, e);
        list<t>::const_iterator cli = l.begin();
        

    6. no implicit conversion from const iterators to iterators

      1. const list<int> il(10);
        list<int>::iterator cil = il.begin();
        --------------------------^
        cxx: Error: t.cc, line 5: no suitable user-defined conversion from
                  "std::list<int, std::allocator<int>>::const_iterator" to
                  "std::list<int, std::allocator<int>>::iterator" exists
        

      2. use the C++ un-const-ing cast const_cast<T>(v)

      3. const list<int> il(10);
        list<int>::iterator cil = const_cast<list<int>::iterator>(il.begin());
        

    7. some containers have only const iterators (set and multiset)


This page last modified on 6 July 2000.