STL学习----入门(1)[forward_list]
来源:互联网 发布:淘宝怎样找货源 编辑:程序博客网 时间:2024/06/05 10:02
学了一半的头文件到这儿了。
#include<forward_list>:
C++11
- // <forward_list>
- template < class T, class Alloc = allocator<T> > class forward_list;
正向列表(Forward list)是一个允许在序列中任何一处位置以常量耗时插入或删除元素的顺序容器(Sequence container)。
A forward_list is a container that supports forward iterators and allows constant time insert and erase operations anywhere within the sequence, with storage management handled automatically.
容性特性:
顺序序列
顺序容器中的元素按照严格的线性顺序排序。可以通过元素在序列中的位置访问对应的元素。
单链表
容器中的每个元素保存了定位前一个元素及后一个元素的信息,允许在任何一处位置以常量耗时进行插入或删除操作,但不能进行直接随机访问(Direct random access)。
能够感知内存分配器的(Allocator-aware)
容器使用一个内存分配器对象来动态地处理它的存储需求。
模板参数
T
存储在容器中的元素的数据类型。
在类模板内部,使用其别名为 value_type 的成员类型。
Alloc
容器内部用来管理内存分配及释放的内存分配器的类型。
这个参数是可选的,它的默认值是 std::allocator<T>,这个是一个最简单的非值依赖的(Value-independent)内存分配器。 在类模板内部,使用其别名为 allocator_type 的成员类型。
详细说明
标准模板库(Standard Template Library,简称STL)中的正向列表容器采用的是单链表(Singly-linked lists)数据库构。单链表可以保存以不同且无关的储存位置容纳的元素。元素间的顺序是通过各个元素中指向下一个元素的链接这一联系维护的。
该特点跟 std:: list 容器的有点相似:主要的区别就是 std:: list 的元素中不仅保存了指向下一个元素的链接,还保存了指向上一个元素的链接,这使得 std::list 允许双向迭代,但消耗更多的存储空间且在插入删除元素时会有稍高(Slight higher)的耗时。因此 std::forward_list 对象比 std::list 对象更高效,尽管它们只能正向迭代。
增加或移动正向列表中的元素不会使指向它们的指针、引用、迭代器失效,只有当对应的元素被销毁时才会如此。
相比较于其它的基本标准顺序容器(数组 std::array、向量 std::vector、双端队列 std::deque 等),正向列表通常在容器中已获得迭代器的任意位置处插入、获得(Extracting,提取)、移动元素等操作中表现出更出色的性能,对那些密集使用上述操作的算法,使用正向列表同样能提升性能,比如排序算法。
双向链表(std::list)及正向链表(std:: forward_list)相比于其它顺序容器的主要缺点是它们不能够通过元素在容器中的位置直接访问(Direct access)元素。举个例子:如果想访问一个正向列表中的第六个元素,那么就必须从一个已知位置(比如开头或末尾)处开始迭代,这会消耗与两个位置之间距间相关的线性时间。而且它们还为保存各个元素间的链接信息消耗更多额外的内存(这点对由小尺寸元素组成而元素数较大的正向列表尤为明显)。
Fast random access to list elements is not supported.
forward_list 类模板是专为极度考虑性能的程序而设计的,它就跟自实现的C型单链表(C-style singly-linked list)一样高效。甚至为了性能,它是唯一一个缺少 size 成员函数的标准容器:这是出于其单链表的性质,如果拥有 size 成员函数,就必须消耗常量时间来维护一个用于保存当前容器大小的内部计数器,这会消耗更多的存储空间,且使得插入及删除操作略微降低性能。如果想获得 forward_list 的大小,可以使用 std::distance 算法且传递 forward_list::begin 及 forward_list::end 参数,该操作的时间复杂度为 O(n)。
对任意列表(std::list)进行插入或删除元素操作需要访问插入位置前的元素,但对 forward_list 来说访问该元素没有常数时间(Constant-time)的方法。因为这个原因,对传递给清除(Erase)、拼接(Splice)等成员函数的范围参数作了修改,这些范围必须为开区间(即不包括末尾元素的同时也不包括起始元素)。
Modifying any list requires access to the element preceding the first element of interest, but in a forward_list there is no constant-time way to acess a preceding element. For this reason, ranges that are modified, such as those supplied to erase and splice, must be open at the beginning.
成员类型
成员类型 定义 value_type第一个模板参数 Tallocator_type第二个模板参数 Allocsize_type无符号整数类型(通常为 size_t)difference_type有符号整数类型(通常为 ptrdiff_t)referencevalue_type&const_referenceconst value_type&pointerstd::allocator_traits<Allocator>::pointerconst_pointerstd::allocator_traits<Allocator>::const_pointeriterator正向迭代器const_iterator常正向迭代器成员函数
(constructor)创建 forward_list(destructor)释放 forward_listoperator=值赋操作Iterators:
before_begin返回指向容器起始位置前的迭代器(iterator)begin返回指向容器起始位置的迭代器end返回指向容器末尾位置的迭代器cbefore_begin返回指向容器起始位置前的常迭代器(const_iterator)cbegin返回指向容器起始位置的常迭代器cend返回指向容器末尾位置的常迭代器Capacity:
empty判断是否为空max_size返回 forward_list 支持的最大元素个数Element access:
front访问第一个元素Modifiers:
assign将新的内容赋值给容器emplace_front 在容器开头构造及插入一个元素push_front在容器开头插入一个元素pop_front删除第一个元素emplace_after构造及插入一个元素insert_after插入元素erase_after删除元素swap交换内容resize改变有效元素的个数clear清空内容Operations:
splice_after使元素从一个正向列表移动到另一个正向列表remove删除值为指定值的元素remove_if删除满足指定条件的元素unique删除重复值merge合并已排序的正向列表sort为容器中的所有元素排序reverse反转元素的顺序Observers:
get_allocator获得内存分配器非成员函数
operator==、operator!=、operator<、operator<=、operator>、operator>=关系操作符std::swap交换两个正向列表的内容算法相关
<algorithm> 头文件中包含大量与 forward_list 容器相关的算法,常使用的有
搜索算法std::adjacent_find、std::count、
std::count_if、std::find、
std::find_if、std::find_end、
std::find_first_of、std::search、
std::search_n、std::equal、
std::mismatch二分查找(Binary search)std::lower_bound、std::upper_bound、
std::equal_range、std::binary_search集合(Set)操作std::includes、std::set_difference、
std::set_intersection、std::set_union、
std::set_symmetric_difference最大与最小std::min_element、std::max_element字典序比较std::lexicographical_compare排列生成器std::next_permutation、
std::prev_permutation其它操作std::all_of、std::any_of、std::none_of、
std::for_each、std::copy、std::copy_if、
std::copy_n、std::copy_backward、
std::move、std::move_backward、
std::swap_ranges、std::iter_swap、
std::transform、std::replace、
std::replace_if、std::replace_copy、
std::replace_copy_if、std::fill、
std::fill_n、std::generate、
std::generate_n、std::remove、
std::remove_if、std::unique、
std::unique_copy、std::reverse、
std::reverse_copy、std::rotate、
std::rotate_copy、std::random_shuffle、
std::shuffle、std::partition、
std::is_partitioned、std::stable_partition、
std::partition_copy、std::merge
- STL学习----入门(1)[forward_list]
- STL forward_list
- stl::forward_list
- STL学习笔记— —容器forward_list
- C++ STL 学习笔记 list,forward_list
- STL-forward_list(单向链表)
- STL(六)forward_list 单向链表
- STL学习笔记6— —容器forward_list
- STL list和forward_list
- STL容器forward_list
- c++STL(三)顺序容器deque,list,forward_list
- STL学习----入门(1)[algorithm]
- STL学习----入门(1)[deque]
- STL学习----入门(1)[functional]
- STL学习----入门(1)[Vector]
- STL学习----入门(1)[iterator]
- STL学习----入门(1)[array]
- STL学习----入门(1)[list]
- Android 的 Handler 总结
- 适配器(Adapter)——类对象结构型模式
- webservice示例(java)
- 彻底根除“无法安装64位版本的Office,因为在您的PC上找到了以下32位程序:”的问题
- Ugly Number
- STL学习----入门(1)[forward_list]
- 文章标题
- JAVA wait(), notify(),sleep详解
- UVALIVE 3713 Astronauts(2-SAT)
- 同一字符串,MD5加密后出现不同的结果
- Android中Timer使用方法
- Android中View绘制优化之一---- 优化布局层次
- 解决Ubuntu系统中文乱码显示问题
- 从dll得到lib文件