各位大侠:
    最近在学习C++ 中的STL,对于其中的迭代器有些疑问:
1、C++中很多种不同的迭代器:input_iterator,output_iterator,bidirectional_iterator,forward_iterator等等,这些不同的类型其作用如何,如果我想在函数调用中使用这些东东,一般实参的定义应该是什么格式?请大家多多指教,提供资料(网页)也可,谢谢!

解决方案 »

  1.   

    <<STL源码剖析>>, 学STL必读
    http://product.dangdang.com/product.aspx?product_id=7405371
      

  2.   

    input iterator所指对象只读, 只能++
    output iterator 只写, 只能++
    forward iterator 可读写, 只能++
    bidiectional iterator, 迭代器可双向移动, 即可++, --
      

  3.   

    STL源码剖析里的例子:template <class InputIterator, class Distance)
    void advance(InputIterator& i, Distance n)
    {
        //...
    }实参可以是:
    stl::vector<int>::iterator. 因为stl::vector<int>::iterator是random_access_iterator_tag, 也肯定是InputIterator见这些iterator的定义struct input_iterator_tag {};
    struct output_iterator_tag {};
    struct forward_iterator_tag : public input_iterator_tag {};
    struct bidirectional_iterator_tag : public forward_iterator_tag {};
    struct random_access_iterator_tag : public bidirectional_iterator_tag {};
      

  4.   

    <C++标准程序库>侯捷怎么的。讲的非常细致!
      

  5.   

    http://www.cplusplus.com/reference/misc/iterator/InputIterator.html
      

  6.   

    以下文章有总结http://www.cnblogs.com/NeuqUstcIim/archive/2008/09/18/1293578.htmlhttp://liulinxia02.blog.163.com/blog/static/26868772008921104448794/
      

  7.   

    参考MSDN,挺细滴
    Iterators are a generalization of pointers, abstracting from their requirements in a way that allows a C++ program to work with different data structures in a uniform manner. Iterators act as intermediaries between containers and generic algorithms. Instead of operating on specific data types, algorithms are defined to operate on a range specified by a type of iterator. Any data structure that satisfies the requirements of the iterator may then be operated on by the algorithm. There are five types or categories of iterator, each with its own set of requirements and resulting functionality: Output: forward moving, may store but not retrieve values, provided by ostream and inserter.Input: forward moving, may retrieve but not store values, provided by istream.Forward: forward moving, may store and retrieve values.Bidirectional: forward and backward moving, may store and retrieve values, provided by list, set, multiset, map, and multimap.Random access: elements accessed in any order, may store and retrieve values, provided by vector, deque, string, and array.Iterators that have greater requirements and so more powerful access to elements may be used in place of iterators with fewer requirements. For example, if a forward iterator is called for, then a random-access iterator may used instead.