Cpp Standard Library 简明教程

C++ Library - <forward_list>

Introduction

正向列表是一个普遍使用的顺序容器。容器是一个保存相同类型数据的对象。正向列表容器被实现为单链表,因此它提供了对其数据的单向顺序访问。

forward_list is a popularly used sequence container. Container is an object that holds data of same type. forward_list container is implemented as singly linked-list, hence it provides unidirectional sequential access to it’s data.

正向列表不提供快速的随机访问,它只支持单向的顺序访问。正向列表允许在序列中的任何地方进行插入和删除操作,且时间为常量。

forward_list doesn’t provide fast random access, it only supports sequential access in only one directions. forward_list allows insertion and deletion operation anywhere within a sequence in constant time.

正向列表的元素可能散布在内存的不同块中。容器存储必要的信息以允许对数据进行顺序访问。正向列表可以在运行时根据需要从两端收缩或扩展。存储需求由内部分配器自动满足。

Elements of forward_list can be scattered in different chunks of memory. Container stores necessary information to allow sequential access to it’s data. forward_lists can shrink or expand as needed from both ends at run time. The storage requirement is fulfilled automatically by internal allocator.

大小为零的正向列表也是有效的。在那种情况下,forward_list.begin() 和 forward_list.end() 指向同一位置。但调用 front() 的行为是不确定的。

Zero sized forward_lists are also valid. In that case forward_list.begin() and forward_list.end() points to same location. But behavior of calling front() is undefined.

Definition

以下是 <forward_list> 头文件中 std::forward_list 的定义

Below is definition of std::forward_list from <forward_list> header file

template < class T, class Alloc = allocator<T> > class forward_list;

Parameters

  1. T − Type of the element contained. T may be substituted by any other data type including user-defined type.

  2. Alloc − Type of allocator object. By default, the allocator class template is used, which defines the simplest memory allocation model and is value-independent.

Member types

以下成员类型可作为参数或返回类型,由成员函数使用。

Following member types can be used as parameters or return type by member functions.

Sr.No.

Member types

Definition

1

value_type

T (First parameter of the template)

2

allocator_type

Alloc (Second parameter of the template)

3

reference

value_type&

4

const_reference

const value_type&

5

pointer

value_type*

6

const_pointer

const value_type*

7

iterator

a random access iterator to value_type

8

const_iterator

a random access iterator to const value_type

9

size_type

size_t

10

difference_type

ptrdiff_t

Functions from <forward_list>

以下是 <forward_list> 头文件的所有方法列表。

Below is list of all methods from <forward_list> header.

Constructors

Sr.No.

Method & Description

1

forward_list::forward_list default constructorConstructs an empty forward_list with zero elements.

2

forward_list::forward_list fill constructorConstructs a new forward_list with n elements and assigns val to each element of forward_list.

3

forward_list::forward_list fill constructorConstructs a new forward_list with n elements and assign zero value to each element of forward_list.

4

forward_list::forward_list range constructorConstructs a forward_list with as many elements as in range of first to last.

5

forward_list::forward_list copy constructorConstructs a forward_list with copy of each elements present in existing list.

6

forward_list::forward_list move constructorConstructs a forward_list with the contents of other using move semantics.

7

forward_list::forward_list initializer list constructorConstructs a forward_list from initialize list.

Destructor

Sr.No.

Method & Description

1

forward_list::~forward_listDestroys forward_list object by deallocating it’s memory.

Member functions

Sr.No.

Method & Description

1

forward_list::assign range versionAssigns new value to forward_list by replacing old ones.

2

forward_list::assign fill versionAssigns new values to forward_list by replacing old ones.

3

forward_list::assign initializer list versionAssigns new values to forward_list by replacing old ones.

4

forward_list::before_beginReturns a random access iterator which points to the position before the first element of the forward_list.

5

forward_list::beginReturns a random access iterator which points to the first element of the forward_list.

6

forward_list::cbefore_beginReturns a constant random access iterator which points to the position before the first element of the forward_list.

7

forward_list::cbeginReturns a constant random access iterator which points to the beginning of the forward_list.

8

forward_list::cendReturns a constant random access iterator which points to the end of the forward_list.

9

forward_list::clearDestroys the forward_list by removing all elements from the forward_list and sets size of forward_list to zero.

10

forward_list::emplace_afterConstructs and inserts new element after position in the forward_list and increases size of forward_list by one.

11

forward_list::emplace_frontInserts new element at the beginning of the forward_list and increases size of forward_list by one.

12

forward_list::emptyTests whether forward_list is empty or not.

13

forward_list::endReturns a random access iterator which points to the last element of the forward_list.

14

forward_list::erase_after position versionRemoves single element after position from the the forward_list.

15

forward_list::erase range versionRemoves range of element from the the forward_list.

16

forward_list::frontReturns a reference to the first element of the forward_list.

17

forward_list::get_allocatorReturns an allocator associated with forward_list

18

forward_list::insert_after single element versionExtends iterator by inserting new element after position in forward_list

19

forward_list::insert_after move versionExtends iterator by inserting new element after position in forward_list

20

forward_list::insert_after fill versionExtends iterator by inserting new element after position in forward_list

21

forward_list::insert_after range versionExtends iterator by inserting new element after position in forward_list

22

forward_list::insert_after initializer list versionExtends iterator by inserting new element after position in forward_list

23

forward_list::max_sizeReturns the maximum number of elements can be held by forward_list.

24

forward_list::mergeMerges two sorted forward_lists into one.

25

forward_list::merge move versionMerges two sorted forward_lists into one.

26

forward_list::merge compare functionMerges two sorted forward_lists into one.

27

forward_list::merge compare function move versionMerges two sorted forward_lists into one by using move semantics.

28

forward_list::operator= copy version Assigns new contents to the forward_list by replacing old ones.

29

forward_list::operator= move version Assign new contents to the forward_list by replacing old ones.

30

forward_list::operator= initializer list version Assign new contents to the forward_list by replacing old ones.

31

forward_list::pop_frontRemoves first element from forward_list.

32

forward_list::push_front copy versionInserts new element at the beginning of forward_list.

33

forward_list::push_front move versionInserts new element at the beginning of forward_list.

34

forward_list::removeremoves element(s) from the forward_list that matches the value.

35

forward_list::remove_ifremoves elements from the forward_list that fulfills the condition.

36

forward_list::resizeChanges the size of forward_list.

37

forward_list::resize value versionChanges the size of forward_list.

38

forward_list::reverseReverses the order of the elements present in the forward_list.

39

forward_list::sortSorts the elements of the forward_list.

40

forward_list::sort compare functionSorts the elements of the forward_list.

41

forward_list::splice_afterTransfers all elements from forward_list to *this.

42

forward_list::splice_after move version Transfers all elements from forward_list x to *this by using move semantics.

43

forward_list::splice_after single elementTransfers a element pointed to by iterator i from forward_list x into *this.

44

forward_list::splice_after single element move version Transfers the element pointed to by iterator i from forward_list x into *this by using move semantics.

45

forward_list::splice_after range version Transfers the elements in the range of first to last from x to *this.

46

forward_list::splice range and move version Transfers the elements in the range of first to last from x to *this by using move semantics.

47

forward_list::swapExchanges the content of a forward_list with contents of another forward_list x.

48

forward_list::uniqueRemoves all consecutive duplicate elements from the forward_list.

49

forward_list::uniqueRemoves all consecutive duplicate elements from the forward_list.

Non-member overloaded functions

Sr.No.

Method & Description

1

[role="bare"]../cpp_standard_library/cpp_forward_list_operator_equal_to.htmlTests whether two forward_lists are equal or not.

2

operator!=Tests whether two forward_lists are equal or not.

3

operator<Tests whether first forward_list is less than other or not.

4

operator⇐Tests whether first forward_list is less than or equal to other or not.

5

operator>Tests whether first forward_list is greater than other or not.

6

operator>=Tests whether first forward_list is greater than or equal to other or not.

7

swapExchanges the contents of two forward_list.