当前位置:首页>科技>stl 迭代器获取数据CSTL容器数据结构
发布时间:2026-07-21阅读(2)
C STL并没有使用c 面向对象的继承与多态技术,而是使用模板和更多的中间层(迭代器、萃取器)实现泛型编程,我来为大家科普一下关于stl 迭代器获取数据?以下内容希望对你有帮助!

stl 迭代器获取数据
C STL并没有使用c 面向对象的继承与多态技术,而是使用模板和更多的中间层(迭代器、萃取器)实现泛型编程。
STL容器模板类实现了常用数据结构,STL算法使用模板函数来实现。算法并不是通过容器类对象作为参数来操作容器,而是通过一个中间层(迭代器)来对容器的元素进行操作。迭代器是容器的内部类,各容器的迭代器类使用统一的接口(重载一些指针算术运算操作符)。容器提供begin()函数和end()函数来返回一对首端、尾端迭代器。而STL算法函数通常使用一对首端、尾端迭代器做参数,一些STL算法函数也可以返回迭代器。
容器的迭代器类重载的操作符不同,容器适用的STL算法也不相同(不同的算法需要不同的重载了不同操作符的迭代器。
支持随机访问迭代器(重载了支持指针算术运算的全部操作符)的容器可以与STL中的所有算法一起使用。例如vector、deque,便适用全部STL算法。
The header <algorithm> defines a collection of functions especially designed to be used on ranges of elements.A range is any sequence of objects that can be accessed through iterators or pointers, such as an array or an instance of some of the STL containers. Notice though, that algorithms operate through iterators directly on the values, not affecting in any way the structure of any possible container (it never affects the size or storage allocation of the container).
根据迭代器重载操作符的不同,迭代器可以分以input、output、forward、indirectional、random access等五类迭代器。
1 迭代器分类1.1 input iterator
Used to read an element from a container. An input iterator can move only in the forward direction (i. e. , from the beginning of the container to the end) one element at a time. Input iterators support only one-pass algorithms—the same input iterator cannot be used to pass through a sequence twice.
1.2 output iterator
Used to write an element to a container. An output iterator can move only in the forward direction one element at a time. Output iterators support only one-pass algorithms—the same output iterator cannot be used to pass through a sequence twice.
1.3 forward iterator
Combines the capabilities of input and output iterators and retains their position in the container (as state information).
1.4 bidirectional iterator
Combines the capabilities of a forward iterator with the ability to move in the backward direction (i. e. , from the end of the container toward the beginning). Bidirectional iterators support multipass algorithms.
1.5 random access iterator
Combines the capabilities of a bidirectional iterator with the ability to directly access any element of the container, i. e. , to jump forward or backward by an arbitrary number of elements. These iterators have a similar functionality to standard pointers (pointers are iterators of this category).
五类迭代器之间并不是继承关系,而是平行和层次关系,intput和output类迭代器是平行关系,其它3类迭代器是层次关系,下层包含上层的全部操作能力(combines the capabilities):
Copyright © 2024 有趣生活 All Rights Reserve吉ICP备19000289号-5 TXT地图HTML地图XML地图