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 n elements forward (or backward, if n is negative)
iter-=n Steps n elements backward (or forward, if n is negative)
iter+n Returns the iterator of the n th next element
n+iter Returns the iterator of the n th next element
iter-n Returns the iterator of the n th 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());
#include <iterator>头文件:流,插入,翻转。
有五种迭代器的种类:
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因为iter++会产生一个临时变量,影响效率。
Out Iterator
Output iterators 它只能向前遍历只写,因此你能赋一个新值only element-by-element ,你不能在同一个范围遍历两次,目的是为了写一个值into a "black hole“,因此,如果你在同一个位置写两次,不保证你能覆盖原来的值。
通常迭代器的都能读或写。一个的纯的Out Iterator是写到 标准输出的迭代器。如果你有两个OutPut Iterator写到屏幕第二个自跟在第一个字后面而不是覆盖它。
Bidirectional Iterators是在Forward Iterators增加了一些操作:
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
- STL迭代器之迭代器的种类
- stl迭代器的种类
- STL迭代器种类
- STL迭代器的种类和简介
- C++中STL迭代器的种类和简介
- C++中STL迭代器的种类和简介
- C++中STL迭代器的种类和简介
- 数据结构实验之查找三:树的种类统计 (STL)
- C++中STL迭代器的种类和简介http://blog.csdn.net/woshifeixingzhuiyue/article/details/7376308
- STL 5: STL之迭代器
- STL之迭代器的类型介绍
- STL之迭代器的使用注意事项
- STL之迭代器
- STL 之 迭代器
- STL 迭代器之配接器
- STL源码之迭代器
- 迭代器之Adapters(STL)
- STL源码之迭代器
- Quartz2D-1(画文字、图片)
- node.js自学笔记(1)-Helloworld
- STL容器之Map,MulitMap
- STL容器之泛型容器
- Tab切换
- STL迭代器之迭代器的种类
- Centos6.5下实现Rsync同步
- QT 中添加资源图片
- Android 内容提供者——Content Providers(一)
- Contiguous Memory Allocator (CMA) 源码分析
- 简单易懂的Dancing links讲解(3)
- 从网络上下载图片的简单小例子【附源码】
- 最好的海外推广公司找哪家?
- 网络笔记