淘客熙熙

主题:【请教】 请哪位大虾简要介绍一下STL中几种iterator -- 东方射日

共:💬3 🌺2
分页树展主题 · 全看首页 上页
/ 1
下页 末页
  • 家园 【请教】 请哪位大虾简要介绍一下STL中几种iterator

    请哪位大虾简要介绍一下STL中几种iterator的不同之处。用法上有什么主意事项。 **有鲜花伺候!**

    Input Iterator

    Output Iterator

    Forward Iterator

    Bidirectional Iterator

    Random Access Iterator

    • 家园 我从"STL Programming Guide"摘录些你看看.

      觉得它解释的很好.

      [Input Iterator]An Input Iterator is an iterator that may be dereferenced to refer to some object, and that may be incremented to obtain the next iterator in a sequence. Input Iterators are not required to be mutable.

      [Output Iterator]An Output Iterator is a type that provides a mechanism for storing (but not necessarily accessing) a sequence of values. Output Iterators are in some sense the converse of Input Iterators, but they have a far more restrictive interface: they do not necessarily support member access or equality, and they do not necessarily have either an associated distance type or even a value type [1]. Intuitively, one picture of an Output Iterator is a tape: you can write a value to the current location and you can advance to the next location, but you cannot read values and you cannot back up or rewind.

      [Forward Iterator]A Forward Iterator is an iterator that corresponds to the usual intuitive notion of a linear sequence of values. It is possible to use Forward Iterators (unlike Input Iterators and Output Iterators) in multipass algorithms. Forward Iterators do not, however, allow stepping backwards through a sequence, but only, as the name suggests, forward.

      [Bidirectional Iterator]A Bidirectional Iterator is an iterator that can be both incremented and decremented. The requirement that a Bidirectional Iterator can be decremented is the only thing that distinguishes Bidirectional Iterators from Forward Iterators.

      [Random Access Iterator]A Random Access Iterator is an iterator that provides both increment and decrement (just like a Bidirectional Iterator), and that also provides constant-time methods for moving forward and backward in arbitrary-sized steps. Random Access Iterators provide essentially all of the operations of ordinary C pointer arithmetic.

      关键词(Tags): #STL#Iterator
    • 家园 Not an expert... but hope this

      helps:

      You usually don't need to pay attention to the iterators unless you are writing generic libraries.

      In the STL, iterators connect algorithms to containers, behind the scene. As suggested by their names, iterator types differ in their capability.

      If what you do is writing applications, knowing the capability of containers and algorithms is more important and adequate most of time.

      If you are writing libraries, say developing a new algorithm, or a new kind of container, that's another story. Because, as aforementioned, iterators enable algorithms to be used on containers generically, you'd have to know precisely how one kind of iterator is different from another to make your new algorithm or new container generic.

分页树展主题 · 全看首页 上页
/ 1
下页 末页


有趣有益,互惠互利;开阔视野,博采众长。
虚拟的网络,真实的人。天南地北客,相逢皆朋友

Copyright © cchere 西西河