STL中标准的迭代器接口
2012-08-16 15:15
99 查看
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 types
iterator_category(a synonym for the template parameter
C),
value_type(a synonym for the template parameter
T), and
distance_type(a synonym for the template parameter
Dist).
iterator
Iterator definitions//迭代器定义:In C++, an iterator is any object that, pointing to some element in a range of elements (such as an array or a
container), 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, each
container 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 different
iterator categories exist:
Iterator categories
Iterators are classified in five categories depending on the functionality they implement:RandomAccess | Bidirectional | Forward | Input | |||
Output |
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 of
input 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. All
standard containers support at least bidirectional iterators types.
Random access iterators implement all the functionalities of
bidirectional 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:
category | characteristic | valid expressions | |||
---|---|---|---|---|---|
all categories | Can be copied and copy-constructed | X b(a); b = a; | |||
Can be incremented | ++a a++ *a++ | ||||
Random Access | Bidirectional | Forward | Input | Accepts equality/inequality comparisons | a == b a != b |
Can be dereferenced as an rvalue | *a a->m | ||||
Output | Can be dereferenced to be the left side of an assignment operation | *a = t *a++ = t | |||
Can be default-constructed | X a; X() | ||||
Can be decremented | --a a-- *a-- | ||||
Supports arithmetic operators + and - | a + n n + a a - n a - b | ||||
Supports inequality comparisons (<, >, <= and >=) between iterators | a < b a > b a <= b a >= b | ||||
Supports compound assignment operations += and -= | a += n a -= n | ||||
Supports offset dereference operator ([]) | a |
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 of
random access iterators's.
Forward iterators have a subset of
bidirectional iterators's. And
input and
output have each their own subset of
forward iterator's.
Base
iteratorIterator base class (class template)iterator_traitsIterator traits (class template)
Functions
Iterator operations:advanceAdvance iterator (function template)
distanceReturn distance between iterators (function template )
Inserters:
back_inserterConstruct a back insert iterator (function template)
front_inserterConstructs a front insert iterator (function template)
inserterConstruct an insert iterator (function template)
Predefined iterators
reverse_iteratorReverse iterator (class template)Inserter iterators
back_insert_iteratorBack insert iterator (class template )
front_insert_iteratorFront insert iterator (class template)
insert_iteratorInsert iterator (class template)
Input/Output iterators
istream_iteratorIstream iterator (class template)
ostream_iteratorOstream iterator (class template )
istreambuf_iteratorInput stream buffer iterator (class template)
ostreambuf_iteratorOutput stream buffer iterator (class template)
相关文章推荐
- 关于stl标准容器中的迭代器的使用注意事项
- stl:空间配置器的标准接口
- STL 容器和迭代器连载7_容器大小的操作
- 20170330STL010_STL_迭代器
- 工具接口标准(TIS)可执行链接格式(ELF)规范-卷I-符号表(Symbol Table)
- Import Standard Purchase Order (标准PO订单导入接口)Bug集合
- 第三章 标准库类型(part 3) 迭代器
- stl 容器适配器、迭代器适配器和函数适配器讲解
- C++:STL标准入门汇总
- 工具接口标准(TIS)可执行链接格式(ELF)规范-卷III-附录A-Intel架构和System V Release 4依赖(二)
- STL迭代器之迭代器的种类
- STL--迭代器失效
- STL中迭代器与traits编程的技巧
- C++学习篇——C++ STL中迭代器介绍(收集)
- STL源码(三)迭代器
- C++:STL标准入门汇总
- [代码分享]静态链表,接口仿照STL设计,操作未做封装
- 几种典型接口的电平标准
- 数据结构(二)标准模版库STL
- C++_STL_iterator(迭代器)