public member function
<forward_list>
empty (default) (1) |
explicit forward_list ( const allocator_type& alloc = allocator_type() );
|
---|
copy (2) |
forward_list ( const forward_list& fwdlst );
forward_list ( const forward_list& fwdlst, const allocator_type& alloc );
|
---|
move (3) |
forward_list ( forward_list&& fwdlst );
forward_list ( forward_list&& fwdlst, const allocator_type& alloc );
|
---|
size (4) |
explicit forward_list ( size_type n );
|
---|
fill (5) |
explicit forward_list ( size_type n, const value_type& val, const allocator_type& alloc = allocator_type() );
|
---|
range (6) |
template < class InputIterator >
forward_list ( InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type() );
|
---|
initializer list (7) |
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) copy constructors
- The object is initialized to have the same contents and properties as the fwdlst forward_list object.
- (3) move constructors
- The object acquires the contents of the rvalue fwdlst.
- (4) size constructor
- Initializes the container object with a size of n elements. Each object is default-constructed.
- (5) fill constructor
- Initializes the container object with a size of n elements, filled with copies of val.
- (6) range constructor
- Constructs a forward_list object containing copies of each of the elements in the range [first,last), in the same order.
- (7) initializer list constructor
- Initializes the container with the contents of the list.
Parameters
- alloc
- Allocator object to be used instead of constructing a new one.
For class instantiations using their version of the default allocator class template, this parameter is not relevant.
Member type allocator_type is defined in forward_list as an alias of its second template parameter (Alloc).
- fwdlst
- Another forward_list object of the same type (with the same class template arguments), whose contents are either copied or moved.
- 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 will be initialized to a copy of this object.
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 type can be any type of input iterator.
- 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 25 26 27 28 29 30 31 32 33
|
// 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::cout << "first:"; for (int& x: first) std::cout << " " << x; std::cout << std::endl;
std::forward_list<int> second (4); // fill: 4 "default-constructed" int's
std::cout << "second:"; for (int& x: second) std::cout << " " << x; std::cout << std::endl;
std::forward_list<int> third (3,77); // fill: 3 sevety-sevens
std::cout << "third:"; for (int& x: third) std::cout << " " << x; std::cout << std::endl;
std::forward_list<int> fourth (third.begin(), third.end()); // range initialization
std::cout << "fourth:"; for (int& x: fourth) std::cout << " " << x; std::cout << std::endl;
std::forward_list<int> fifth (fourth); // copy constructor
std::cout << "fifth:"; for (int& x: fifth) std::cout << " " << x; std::cout << std::endl;
std::forward_list<int> sixth (std::move(fifth)); // move ctor. (fifth wasted)
std::cout << "sixth:"; for (int& x: sixth) std::cout << " " << x; std::cout << std::endl;
std::forward_list<int> seventh = {3, 52, 25, 90}; // initializer_list constructor
std::cout << "seventh:"; for (int& x: seventh) std::cout << " " << x; std::cout << std::endl;
std::cout << std::endl;
return 0;
}
|
Possible output:
forward_list constructor examples:
first:
second 0 0 0 0
third: 77 77 77
fourth: 77 77 77
fifth: 77 77 77
sixth: 77 77 77
seventh: 3 52 25 90
|
Complexity
For the default constructor (1), constant.
For the copy constructors (2), linear in fwdlst's size (copy constructions).
For the move constructors (3), constant.
For the size constructor (4), linear in n (default constructions).
For the fill constructor (5), linear in n (copy constructions).
For the range constructor (6), linear in the distance between the iterators (copy constructions).
For the initializer list constructor (7), linear in the length of the initializer list (copy constructions).