一、forward_list简介
template<
class T,
class Allocator = std::allocator<T>
> class forward_list;
参数介绍:
T:包含的元素的类型,即array::value_type。
Allocator:一个分配器,用于获取/释放内存以及构造/销毁该内存中的元素
forward_list是一个序列容器,它的底层实现为单向链表,允许在序列中的任何位置进行恒定时间的插入和擦除操作,但不支持快速随机访问。它与std::list(双向链表)相比,此容器在不需要每个元素消耗额外的存储空间来保存前节点的信息,因此更加节省空间。
在forward_list中或跨多个forward_list添加、删除和移动元素不会使当前引用forward_list中其他元素的迭代器无效。但是,当从forward_list中删除相应的元素(通过erase_after )时,引用元素的迭代器或引用无效。
另外,forward_list是唯一一个出于效率考虑故意缺少大小成员函数的标准容器,所以要获取forward_list对象的大小,必须参考容器的开始及结束位置,进行线性遍历计算
二、forward_list的成员函数
1、迭代器
iterator begin(); // 返回容器的迭代器到第一个元素
const_iterator cbegin(); // 返回容器的迭代器到第一个元素
iterator end(); // 返回容器的迭代器到最后一个元素
const_iterator cend(); // 返回容器的迭代器到最后一个元素
// 返回指向容器第一个元素之前的元素的迭代器。此元素充当占位符,尝试访问它会导致未定义的行为
iterator before_begin() noexcept;
const_iterator before_begin() const noexcept;
const_iterator cbefore_begin() const noexcept;
2、容量相关
bool empty(); // 判断容器是否为空
size_type max_size(); //返回容器内可存放的最大元素个数
3、容器元素
// 清除容器中的所有元素
void clear() noexcept;
// 在pos位置插入元素value,返回新插入元素的迭代器
iterator insert_after( const_iterator pos, const T& value );
iterator insert_after( const_iterator pos, T&& value );
// 在pos位置插入count个元素value,返回最后新插入元素的迭代器,若count为0,则返回pos
iterator insert_after( const_iterator pos, size_type count, const T& value );
// 在pos位置插入[first,last ),返回最后新插入元素的迭代器,若first==last,则返回pos
template< class InputIt >
iterator insert_after( const_iterator pos, InputIt first, InputIt last );
// 在pos位置插入ilist,若ilist 为空,则返回pos
iterator insert_after( const_iterator pos, std::initializer_list<T> ilist );
// 将新元素插入到容器中指定位置之后的位置。元素是就地构造的,即不执行复制或移动操作。args为构造其元素的参数
template< class... Args >
iterator emplace_after( const_iterator pos, Args&&... args );
// 在容器的开头插入一个新元素,args为构造其元素的参数
template< class... Args >
void emplace_front( Args&&... args );
// 移除容器的第一个元素。如果容器中没有元素,则行为未定义。
void pop_front();
// 将给定元素添加value到容器的开头。
void push_front (const value_type& val);
void push_front (value_type&& val);
// 从容器中移除pos位置元素
iterator erase_after( const_iterator pos );
// 从容器中移除[first,last)位置元素
iterator erase_after( const_iterator first, const_iterator last );
// 修改容器大小为count,若count小于当前容器元素个数,则从末尾开始移除。若大于count,则扩展新元素
// (若value指定则使用value副本进行初始化,若value未指定,则使用默认参数进行构造)
void resize( size_type count );
void resize( size_type count, const value_type& value );
// 交换两容器的值,此时不涉及元素的拷贝赋值等
void swap( forward_list& other );
5、容器自身修改
// 将other容器中的元素合并入该容器中,此时两个容器都应该已经排序。之后other将为空
void merge( forward_list& other );
void merge( forward_list&& other );
// 将other容器中的元素合并入该容器中,使用comp进行比较,此时两个容器都应该已经排序。这有效地删除了other
template <class Compare>
void merge( forward_list& other, Compare comp );
template <class Compare>
void merge( forward_list&& other, Compare comp );
// 将other中所有元素转移到该容器中,将它们插入到position指向的元素之后。
void splice_after ( const_iterator pos, forward_list & other ) ;
void splice_after ( const_iterator pos, forward_list && other ) ;
// 将other中的it位置元素转移到该容器中,将它们插入到position指向的元素之后
void splice_after ( const_iterator pos, forward_list & other,
const_iterator it ) ;
void splice_after ( const_iterator pos, forward_list && other,
const_iterator it ) ;
// 将other中的(first,last]位置元素转移到该容器中,将它们插入到position指向的元素之后
void splice_after ( const_iterator pos, forward_list & other,
const_iterator first, const_iterator last ) ;
void splice_after ( const_iterator pos, forward_list && other,
const_iterator first, const_iterator last ) ;
// 删除容器中元素值为val的元素
void remove (const value_type& val);
// 删除容器中pred函数返回true的元素素
template <class Predicate>
void remove_if (Predicate pred);
// 从容器中每个连续的相等元素组中删除除第一个元素之外的所有元素
void unique();
// 删除二元谓词binary_pred返回true的元素
template <class BinaryPredicate>
void unique (BinaryPredicate binary_pred);
// 对容器内元素进行排序,默认增序
void sort();
// 使用二元谓词comp对容器进行排序
template <class Compare>
void sort (Compare comp);
//反转容器内元素位置
void reverse() noexcept;