一、简介
forward_list是一个单向链表。只支持单向顺序访问,在链表的任何位置进行插入/删除元素的速度都很快。
和list一样,forward_list中间的元素也存放在多个不同的且没有联系的存储空间当中,通过前一个元素到后一个元素之间的链接来实现顺序访问。forward_list也不支持下标操作符访问,要想遍历forward_list中的元素,则需要使用迭代器。
forward_list与其他标准容器最大的区别就在于,他没有size函数。获取容器的size,其内部需要有一个size计数器来一直记录着容器大小的变化,这样使得时间开销和内存开销都会增加,从而降低了forward_list的工作效率。想要获取forward_list的容器大小,可以使用algorithm头文件当中的diatance函数测量指向begin位置的迭代器和指向end位置的迭代器之间的距离即可。
forward_list <value_type> fwlist;
int size_fwlist = diatance(fwlist.begin(),fwlist.end());
二、容器特征
1.顺序容器
2.单链表
3.动态内存分配
三、模板参数
1.T
2.Alloc
四、类型
member type |
definition | notes |
---|---|---|
value_type | The first template parameter (T) | |
allocator_type | The second template parameter (Alloc) | defaults to: allocator<value_type> |
reference | value_type& | |
const_reference | const value_type& | |
pointer | allocator_traits<allocator_type>::pointer | for the default allocator: value_type* |
const_pointer | allocator_traits<allocator_type>::const |