public member function
<forward_list>
| default (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()); |
|---|
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 and a size of zero.
- (2) fill constructors
- Initializes the container object with a size of n elements. Each object is a copy if val (if provided), or value-initialized (if val is not provided).
- (3) range constructor
- Constructs a forward_list object containing copies of each of the elements in the range [first,last), in the same order.
- (4) copy constructor (and copying with allocator)
- The object is initialized to have the same contents and properties as the fwdlst forward_list object.
- (5) move constructor (and moving with allocator)
- The object acquires the contents of fwdlst.
- (6) initializer list constructor
- Initializes the container with the contents of the il list.
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).
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.
Parameters
- alloc
- Allocator object.
The container keeps and uses an internal copy of this allocator.
Member type allocator_type is the internal allocator type used by the container, defined in forward_list as an alias of its second template parameter (Alloc).
If allocator_type is 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).
Member type size_type is an unsigned integral type.
- val
- Value to fill the container with. Each of the n elements in the container is initialized to a copy of this value.
Member type value_type is the type of the elements in the container, defined in forward_list as an alias of its first template parameter (T).
- first, last
- Input iterators to the initial and final positions in a range. The range used is [first,last), which includes all the elements between first and last, including the element pointed by first but not the element pointed by last.
The function template argument InputIterator shall be an input iterator type that points to elements implicitly convertible to value_type.
- fwdlst
- Another forward_list object of the same type (with the same class template arguments), whose contents are either copied or moved.
- il
- An initializer_list object.
These objects are automatically constructed from initializer list declarators.
Member type value_type is the type of the elements in the container, defined in forward_list as an alias of its first template parameter (T).
Example
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 (first.begin(), first.end()); // range initialization
std::forward_list<int> fourth (fourth); // 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;
}
|
Possible output:
forward_list constructor examples:
first:
second: 77 77 77
third: 77 77 77
fourth: 77 77 77
fifth: 77 77 77
sixth: 3 52 25 90
|
Complexity
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.
Data races
All copied elements are accessed.
The move constructors (5) modify fwdlst.
Exception safety
Strong guarantee: no effects in case an exception is thrown.
It throws if any operation on the elements, the allocator or the iterators throws.