STL中标准的迭代器接口
来源:互联网 发布:数控编程员工资 编辑:程序博客网 时间:2024/06/06 07:33
template<class C, class T, class Dist = ptrdiff_t> struct iterator { typedef C iterator_category; typedef T value_type; typedef Dist distance_type; };
The template class serves as a base type for all iterators. It defines the member typesiterator_category
(a synonym for the template parameterC
), value_type
(a synonym for the template parameterT
), and distance_type
(a synonym for the template parameterDist
).
iterator
In C++, an iterator is any object that, pointing to some element in a range of elements (such as an array or acontainer), has the ability to iterate through the elements of that range using a set of operators (at least, the increment (++) and dereference (*) operators).
在c++中,迭代器就是指向特定范围中(比如数组或容器)的某些元素的对象,迭代器拥有通过使用一系列的操作符(至少,自增和解引用操作符)重复操作特定范围里的元素
The most obvious form of iterator is a pointer: A pointer can point to elements in an array, and can iterate through them using the increment operator (++). But other forms of iterators exist. For example, eachcontainer type (such as a vector) has a specific iterator type designed to iterate through its elements in an efficient way.
Notice that while a pointer is a form of iterator, not all iterators have the same functionality a pointer has; To distinguish between the requirements an iterator shall have for a specific algorithm, five differentiterator categories exist:
Iterator categories
Iterators are classified in five categories depending on the functionality they implement:
In this graph, each iterator category implements the functionalities of all categories to its right:
Input and output iterators are the most limited types of iterators, specialized in performing only sequential input or output operations.
Forward iterators have all the functionality ofinput and output iterators, although they are limited to one direction in which to iterate through a range.
Bidirectional iterators can be iterated through in both directions. Allstandard containers support at least bidirectional iterators types.
Random access iterators implement all the functionalities ofbidirectional iterators, plus, they have the ability to access ranges non-sequentially: offsets can be directly applied to these iterators without iterating through all the elements in between. This provides these iterators with the same functionality as standard pointers (pointers are iterators of this category).
The characteristics of each category of iterators are:
b = a;Can be incremented++a
a++
*a++Random AccessBidirectionalForwardInputAccepts equality/inequality comparisonsa == b
a != bCan be dereferenced as an rvalue*a
a->mOutputCan be dereferenced to be the left side of an assignment operation*a = t
*a++ = t Can be default-constructedX a;
X() Can be decremented--a
a--
*a-- Supports arithmetic operators + and -a + n
n + a
a - n
a - bSupports inequality comparisons (<, >, <= and >=) between iteratorsa < b
a > b
a <= b
a >= bSupports compound assignment operations += and -=a += n
a -= nSupports offset dereference operator ([])a[n]
Where X is an iterator type, a and b are objects of this iterator type,t is an object of the type pointed by the iterator type, and n is an integer value.
Random access iterators have all characteristics.Bidirectional iterators have a subset ofrandom access iterators's.Forward iterators have a subset ofbidirectional iterators's. Andinput and output have each their own subset of forward iterator's.
Base
- iterator
- Iterator base class (class template)
- iterator_traits
- Iterator traits (class template)
Functions
Iterator operations:
- advance
- Advance iterator (function template)
- distance
- Return distance between iterators (function template )
Inserters:
- back_inserter
- Construct a back insert iterator (function template)
- front_inserter
- Constructs a front insert iterator (function template)
- inserter
- Construct an insert iterator (function template)
Predefined iterators
- reverse_iterator
- Reverse iterator (class template)
Inserter iterators
- back_insert_iterator
- Back insert iterator (class template )
- front_insert_iterator
- Front insert iterator (class template)
- insert_iterator
- Insert iterator (class template)
Input/Output iterators
- istream_iterator
- Istream iterator (class template)
- ostream_iterator
- Ostream iterator (class template )
- istreambuf_iterator
- Input stream buffer iterator (class template)
- ostreambuf_iterator
- Output stream buffer iterator (class template)
- STL中标准的迭代器接口
- 标准STL中list的各个接口的使用
- 【STL】标准库中vector的操作
- STL中list各个接口的使用
- STL 中list的常用接口
- STL标准库中的list接口
- C++ 11标准STL中Traits的is_pointer的实现
- 【STL】标准库中vector的构造函数
- STL标准容器中内容的几种打印技巧
- 标准C++ 中STL 类的简单介绍
- STL 标准模板库 中栈stack 容器的使用
- STL标准库中vector list deque的区别
- STL中list的各个接口的使用
- Java中接口的实际应用:指定操作的标准
- STL 标准容器的选择
- 再谈Win32动态库的接口中使用STL(原创)
- 探讨jdk标准库中Map接口的put方法
- Java SE 6中 XML 数字签名的标准 Java 接口
- 周立波经典语录
- eqq for shell
- Struts2结合jquery autocomplet插件实现自动提示详解
- 雷锋科普:小米M2之芯高通APQ8064芯片组解析
- 编译tomcat7.x源码
- STL中标准的迭代器接口
- hdu1028
- Android之NDK开发之HelloWorld
- PLC上位机监控系统(摘要)
- 请问exit(0)与exit(1)有什么区别?它们可以通用吗?里面的参数的不同具体体现在那里?
- 堆排序
- Xtragrid的图片显示
- 双调TSP问题总结---poj 2677和usaco5.4 canada tour
- N×N矩阵螺旋打印输出