public member function
<forward_list>

std::forward_list::forward_list

默认 (1)
explicit forward_list (const allocator_type& alloc = allocator_type());
fill (2)
explicit forward_list (size_type n);explicit forward_list (size_type n, const value_type& val,                        const allocator_type& alloc = allocator_type());
range (3)
template <class InputIterator>  forward_list (InputIterator first, InputIterator last,                const allocator_type& alloc = allocator_type());
copy (4)
forward_list (const forward_list& fwdlst);forward_list (const forward_list& fwdlst, const allocator_type& alloc);
move (5)
forward_list (forward_list&& fwdlst);forward_list (forward_list&& fwdlst, const allocator_type& alloc);
initializer list (6)
forward_list (initializer_list<value_type> il,              const allocator_type& alloc = allocator_type());
默认 (1)
forward_list();explicit forward_list (const allocator_type& alloc);
fill (2)
explicit forward_list (size_type n, const allocator_type& alloc = allocator_type());explicit forward_list (size_type n, const value_type& val,                        const allocator_type& alloc = allocator_type());
range (3)
template <class InputIterator>  forward_list (InputIterator first, InputIterator last,                const allocator_type& alloc = allocator_type());
copy (4)
forward_list (const forward_list& fwdlst);forward_list (const forward_list& fwdlst, const allocator_type& alloc);
move (5)
forward_list (forward_list&& fwdlst);forward_list (forward_list&& fwdlst, const allocator_type& alloc);
initializer list (6)
forward_list (initializer_list<value_type> il,              const allocator_type& alloc = allocator_type());
Construct forward_list object
Constructs a forward_list container object, initializing its contents depending on the constructor version used

(1) empty container constructor (default constructor)
Constructs an empty container, with no elements.
(2) fill constructor
Constructs a container with n elements. Each element is a copy of val (if provided).
(3) range constructor
Constructs a container with as many elements as the range[first,last), with each element emplace-constructed from its corresponding element in that range, in the same order.
(4) copy constructor (and copying with allocator)
Constructs a container with a copy of each of the elements in fwdlst, in the same order.
(5) move constructor (and moving with allocator)
Constructs a container that acquires the elements of fwdlst.
If alloc is specified and is different from fwdlst's allocator, the elements are moved. Otherwise, no elements are constructed (their ownership is directly transferred).
fwdlst is left in an unspecified but valid state.
(6) initializer list constructor
Constructs a container with a copy of each of the elements in il, in the same order.

The container keeps an internal copy of alloc, which is used to allocate and deallocate storage for its elements, and to construct and destroy them (as specified by its allocator_traits). If no alloc argument is passed to the constructor, a default-constructed allocator is used, except in the following cases
- The copy constructor (4, first signature) creates a container that keeps and uses a copy of the allocator returned by calling the appropriate selected_on_container_copy_construction trait on fwdlst's allocator.
- The move constructor (5, first signature) acquires fwdlst's allocator.

All elements are copied, moved or otherwise constructed by calling allocator_traits::construct with the appropriate arguments.

参数

alloc
Allocator object.
The container keeps and uses an internal copy of this allocator.
成员类型allocator_typeis the internal allocator type used by the container, defined in forward_list as an alias of its second template parameter (Alloc).
如果已知allocator_typeis an instantiation of the default allocator (which has no state), this is not relevant.
n
Initial container size (i.e., the number of elements in the container at construction).
成员类型size_type是一种无符号整型类型。
val
Value to fill the container with. Each of the n elements in the container is initialized to a copy of this value.
成员类型value_typeis the type of the elements in the container, defined in forward_list as an alias of its first template parameter (T).
first, last
指向范围内初始位置和最终位置的输入迭代器。 使用的范围是[first,last),它包括firstlast之间的所有元素,包括first指向的元素,但不包括last指向的元素。
函数模板参数InputIteratorshall be an input iterator type that points to elements of a type from whichvalue_type对象的类型的元素。
fwdlst
Another forward_list object of the same type (with the same class template arguments), whose contents are either copied or acquired.
il
il
这些对象是从初始化列表声明符自动构造的。
成员类型value_typeis the type of the elements in the container, defined in forward_list as an alias of its first template parameter (T).

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// forward_list constructors
#include <iostream>
#include <forward_list>

int main ()
{
  // constructors used in the same order as described above:

  std::forward_list<int> first;                      // default: empty
  std::forward_list<int> second (3,77);              // fill: 3 seventy-sevens
  std::forward_list<int> third (second.begin(), second.end()); // range initialization
  std::forward_list<int> fourth (third);            // copy constructor
  std::forward_list<int> fifth (std::move(fourth));  // move ctor. (fourth wasted)
  std::forward_list<int> sixth = {3, 52, 25, 90};    // initializer_list constructor

  std::cout << "first:" ; for (int& x: first)  std::cout << " " << x; std::cout << '\n';
  std::cout << "second:"; for (int& x: second) std::cout << " " << x; std::cout << '\n';
  std::cout << "third:";  for (int& x: third)  std::cout << " " << x; std::cout << '\n';
  std::cout << "fourth:"; for (int& x: fourth) std::cout << " " << x; std::cout << '\n';
  std::cout << "fifth:";  for (int& x: fifth)  std::cout << " " << x; std::cout << '\n';
  std::cout << "sixth:";  for (int& x: sixth)  std::cout << " " << x; std::cout << '\n';

  return 0;
}
可能的输出
forward_list constructor examples:
first:
second: 77 77 77
third: 77 77 77
fourth:
fifth: 77 77 77
sixth: 3 52 25 90


复杂度

Constant for the default constructor (1), and for the move constructors (5) (unless alloc is different from fwdlst's allocator).
For all other cases, linear in the number of elements in the resulting container.

迭代器有效性

The move constructors (5), invalidate iterators, pointers and references referring to the elements of x if the elements are moved.

数据竞争

All copied elements are accessed.
The move constructors (5) modify fwdlst.

异常安全

Strong guarantee: no effects in case an exception is thrown.
If allocator_traits::construct is not supported with the appropriate arguments for the element constructions, or if the range specified by[first,last)无效,则会导致未定义行为

另见