STL迭代器之迭代器的种类

来源:互联网 发布:linux curl post 图片 编辑:程序博客网 时间:2024/05/16 17:58
所有的容器都有自己的迭代器,我用的时候不需要添加什么文件,但有三种迭代器一定要加
#include <iterator>头文件:流,插入,翻转。
有五种迭代器的种类:
Iterator Category Ability Providers
Input iterator         Reads forward           istream
Output iterator        Writes forward           ostream, inserter
Forward iterator      Reads and writes forward
Bidirectional iterator  Reads and writes forward and list, set, multisetmap,backwardmultimap
Random access iterator Reads and writes with random access vector, deque string, array

Input Iterator
注意:Input Iterator 只能向前读一次,因此,如果你赋值一个input iterator,并让原来的和拷贝的向前读,它们遍历的值有可能不一样。

它有以下的操作:
*iter                  Provides read access to the actual element
iter ->member        Provides read access to a member (if any) of the actual element
++iter               Steps forward (returns new position)
iter++                      Steps forward (returns old position)
Iter1 == iter2          Returns whether two iterators are equal
Iter1 != iter2           Returns whether two iterators are not equal
TYPE(iter)               Copies iterator (copy constructor)
我们应该在遍历时总是用++iter因为iter++会产生一个临时变量,影响效率。

Out Iterator
Output iterators 它只能向前遍历只写,因此你能赋一个新值only element-by-element ,你不能在同一个范围遍历两次,目的是为了写一个值into a "black hole“,因此,如果你在同一个位置写两次,不保证你能覆盖原来的值。
*iter = value     Writes value to where the iterator refers
++iter              Steps forward (returns new position)
iter++             Steps forward (returns old position)
TYPE (iter)      Copies iterator (copy constructor)
通常迭代器的都能读或写。一个的纯的Out Iterator是写到 标准输出的迭代器。如果你有两个OutPut Iterator写到屏幕第二个自跟在第一个字后面而不是覆盖它。

Forward Iterators
它是拥有前两种迭代器的所有能力。
*iter            Provides access to the actual element
iter->           member Provides access to a member of the actual element
++iter          Steps forward (returns new position)
iter++          Steps forward (returns old position)
iter1 == iter2    Returns whether two iterators are equal
iter1 != iter2     Returns whether two iterators are not equal
TYPE()         Creates iterator (default constructor)
TYPE(iter)      Copies iterator (copy constructor)
iter1 = iter2     Assigns an iterator

Bidirectional Iterators
Bidirectional Iterators是在Forward Iterators增加了一些操作:
-- iter     Steps backward (returns new position)
iter--      Steps backward (returns old position)

Random Access Iterators
它在Bidirectional Iterators的基础上增加了一些操作:
iter[n]       Provides access to the element that has index n
iter+=n     Steps elements forward (or backward, if is negative)
iter-=n      Steps elements backward (or forward, if is negative)
iter+n      Returns the iterator of the nth next element
n+iter      Returns the iterator of the nth next element
iter-n       Returns the iterator of the nth previous element
iter1-iter2   Returns the distance between iter1 and iter2
iter1<iter2   Returns whether iter1 is before iter2
iter1>iter2   Returns whether iter1 is after iter2
iter1<=iter2  Returns whether iter1 is not after iter2
iter1>=iter2  Returns whether iter1 is not before iter2
The following program demonstrates the special abilities of random

Vector 前置(++,- -)的错误:
coll.sort (++coll.begin(), coll.end());
这是错误的,因为Vector的迭代器作用就是指针,不可以改变临时指针的值,而对于一个机构体或是一个类是可以的。我们通过引进一个变量来解决:
std::vector<int>::iterator beg = coll.begin();
coll.sort (++beg, coll.end());
0 0
原创粉丝点击