C++ Reference: Standard C++ Library reference: Containers: forward_list: forward_list

C++官网参考链接:https://cplusplus.com/reference/forward_list/forward_list/

类模板
<forward_list>
std::forward_list
template < class T, class Alloc = allocator<T> > class forward_list;
正向链表 
正向链表是顺序容器,它允许在序列中的任何位置进行以常量时间进行的插入和删除操作。
正向链表实现为单链表;单链表可以将它们包含的每个元素存储在不同且不相关的存储位置。顺序是通过与序列中下一个元素的链接的每个元素的关联来保持的。
forward_list容器和list容器在设计上的主要区别在于,前者在内部只保留一个指向下一个元素的链接,而后者在每个元素上保留两个链接:一个指向下一个元素,一个指向前一个元素,允许双向高效迭代,但每个元素消耗额外的存储空间,插入和删除元素的时间开销略高。forward_list对象因此比list对象更有效,尽管它们只能正向迭代。
与其他基本标准顺序容器(arrayvectordeque)相比,forward_list通常在插入、提取和移动容器内任意位置的元素方面表现更好,因此在大量使用这些元素的算法也是如此,例如排序算法。
与其他顺序容器相比,forward_lists和list的主要缺点是它们不能通过位置直接访问元素;例如,要访问forward_list中的第6个元素,必须从开始迭代到该位置,这需要在它们之间的距离的线性时间。它们还消耗一些额外的内存来保持与每个元素相关联的链接信息(这可能是包含小型元素的大型链表的一个重要因素)。
forward_list类模板在设计时考虑到了效率:在设计上,它的效率与简单的手写的C风格单链表一样高,事实上,它是唯一出于效率考虑而故意缺乏size成员函数的标准容器:由于其作为链表的性质,拥有一个占用恒定时间的size成员将要求它为其大小保留一个内部计数器(就像list那样)。这将消耗一些额外的存储,并使插入和删除操作的效率略低。要获得forward_list对象的大小,可以使用其beginenddistance算法,这是一个需要线性时间的操作。

容器的属性
按顺序排列 
顺序容器中的元素按照严格的线性顺序排列。各个元素通过它们在该序列中的位置进行访问。
链表
每个元素都保存关于如何定位下一个元素的信息,允许在特定元素(甚至是整个范围)之后进行固定时间的插入和删除操作,但不能直接随机访问。
能够感知的allocator
容器使用allocator对象动态处理其存储需求。

模板形参 
T
元素的类型。
别名为成员类型forward_list::value_type。
Alloc
用于定义存储分配模型的allocator对象的类型。默认情况下,使用allocator类模板,它定义了最简单的内存分配模型,并且与值无关。
别名为成员类型forward_list::allocator_type。

成员类型

member typedefinitionnotes
value_typeThe first template parameter (T)
allocator_typeThe second template parameter (Alloc)defaults to: allocator<value_type>
referencevalue_type&
const_referenceconst value_type&
pointerallocator_traits<allocator_type>::pointerfor the default allocator: value_type*
const_pointerallocator_traits<allocator_type>::const_pointerfor the default allocator: const value_type*
iteratorforward iterator to value_typeconvertible to const_iterator
const_iteratorforward iterator to const value_type
difference_typea signed integral type, identical to: iterator_traits<iterator>::difference_typeusually the same as ptrdiff_t
size_typean unsigned integral type that can represent any non-negative value of difference_typeusually the same as size_t

成员函数
(constructor)    Construct forward_list object (public member function)
(destructor)    Destroy forward_list object (public member function)
operator=    Assign content (public member function)

iterator
before_begin    Return iterator to before beginning (public member function)
begin    Return iterator to beginning (public member type)
end    Return iterator to end (public member function)
cbefore_begin    Return const_iterator to before beginning (public member function)
cbegin    Return const_iterator to beginning (public member function)
cend    Return const_iterator to end (public member function)

容量
empty    Test whether array is empty (public member function)
max_size    Return maximum size (public member function)

元素访问 
front    Access first element (public member function)

修改器
assign    Assign content (public member function)
emplace_front    Construct and insert element at beginning (public member function)
push_front    Insert element at beginning (public member function)
pop_front    Delete first element (public member function)
emplace_after    Construct and insert element (public member function)
insert_after    Insert elements (public member function)
erase_after    Erase elements (public member function)
swap    Swap content (public member function)
resize    Change size (public member function)
clear    Clear content (public member function)

操作
splice_after    Transfer elements from another forward_list (public member function)
remove    Remove elements with specific value (public member function)
remove_if    Remove elements fulfilling condition (public member function template)
unique    Remove duplicate values (public member function)
merge    Merge sorted lists (public member function)
sort    Sort elements in container (public member function)
reverse    Reverse the order of elements (public member function)

观测器
get_allocator    Get allocator (public member function)

非成员函数重载 
relational operators (forward_list)    Relational operators for forward_list (function template)
swap (forward_list)    Exchanges the contents of two forward_list containers (function template) 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_40186813

你的能量无可限量。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值