STL学习----入门(1)[forward_list]

来源:互联网 发布:淘宝怎样找货源 编辑:程序博客网 时间:2024/06/05 10:02


学了一半的头文件到这儿了。

#include<forward_list>:

C++11

  1. // <forward_list>
  2. 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::beginforward_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_tdifference_type有符号整数类型(通常为 ptrdiff_treferencevalue_type&const_referenceconst value_type&pointerstd::allocator_traits<Allocator>::pointerconst_pointerstd::allocator_traits<Allocator>::const_pointeriterator正向迭代器const_iterator常正向迭代器
  • 成员函数

    (constructor)创建 forward_list(destructor)释放 forward_list​operator=值赋操作

    Iterators:

    before_begin返回指向容器起始位置前的迭代器(iteratorbegin返回指向容器起始位置的迭代器end返回指向容器末尾位置的迭代器cbefore_begin返回指向容器起始位置前的常迭代器(const_iteratorcbegin返回指向容器起始位置的常迭代器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_findstd::count
    std::count_ifstd::find
    std::find_ifstd::find_end
    std::find_first_ofstd::search
    std::search_nstd::equal
    std::mismatch二分查找(Binary search)std::lower_boundstd::upper_bound
    std::equal_rangestd::binary_search集合(Set)操作std::includesstd::set_difference
    std::set_intersectionstd::set_union
    std::set_symmetric_difference最大与最小std::min_elementstd::max_element字典序比较std::lexicographical_compare排列生成器std::next_permutation
    std::prev_permutation其它操作std::all_ofstd::any_ofstd::none_of
    std::for_eachstd::copystd::copy_if
    std::copy_nstd::copy_backward
    std::movestd::move_backward
    std::swap_rangesstd::iter_swap
    std::transformstd::replace
    std::replace_ifstd::replace_copy
    std::replace_copy_ifstd::fill
    std::fill_nstd::generate
    std::generate_nstd::remove
    std::remove_ifstd::unique
    std::unique_copystd::reverse
    ​std::reverse_copystd::rotate
    std::rotate_copystd::random_shuffle
    std::shufflestd::partition
    std::is_partitionedstd::stable_partition
    std::partition_copystd::merge


0 0
原创粉丝点击