Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


The C++ forward_list::forward_list() function is used to construct a forward_list object, initializing its contents depending on the version of constructor used:

Syntax

//default version - construct an empty 
//container with no elements
explicit forward_list (const allocator_type& alloc = allocator_type());

//fill version - construct a container 
//with n elements, each equal to val	
explicit forward_list (size_type n);
explicit forward_list (size_type n, const value_type& val,
                        const allocator_type& alloc = allocator_type());

//range version - Constructs a container with 
//elements as the range [first,last)
template <class InputIterator>
  forward_list (InputIterator first, InputIterator last,
                const allocator_type& alloc = allocator_type());

//copy version - copies all elements
//of fwdlst into the container	
forward_list (const forward_list& fwdlst);
forward_list (const forward_list& fwdlst, const allocator_type& alloc);

//move version - moves elements of fwdlst
//into the container	
forward_list (forward_list&& fwdlst);
forward_list (forward_list&& fwdlst, const allocator_type& alloc);

//initializer list version - copies all 
//elements of il into the container	
forward_list (initializer_list<value_type> il,
              const allocator_type& alloc = allocator_type());
//default version - construct an empty 
//container with no elements
forward_list();
explicit forward_list (const allocator_type& alloc);

//fill version - construct a container 
//with n elements, each equal to val
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 version - Constructs a container with 
//elements as the range [first,last)
template <class InputIterator>
  forward_list (InputIterator first, InputIterator last,
                const allocator_type& alloc = allocator_type());

//copy version - copies all elements
//of fwdlst into the container	
forward_list (const forward_list& fwdlst);
forward_list (const forward_list& fwdlst, const allocator_type& alloc);

//move version - moves elements of fwdlst
//into the container	
forward_list (forward_list&& fwdlst);
forward_list (forward_list&& fwdlst, const allocator_type& alloc);

//initializer list version - copies all 
//elements of il into the container		
forward_list (initializer_list<value_type> il,
              const allocator_type& alloc = allocator_type());

Parameters

alloc Specify the Allocator object. The container keeps and uses an internal copy of this allocator.
n Specify the number of elements in the container at construction.
val Specify the value to fill the container with.
first Specify initial position of the input iterator of the range. The range used is [first,last).
last Specify final position of the input iterator of the range. The range used is [first,last).
fwdlst Specify a forward_list object of same type.
il Specify an initializer_list object.

Return Value

Constructor never returns value.

Time Complexity

Constant i.e, Θ(1), for default version and move version.
For all the other cases, Linear i.e, Θ(n).

Example: Using default and fill version

In the below example, the forward_list::forward_list function is used to construct a forward_list object.

#include <iostream>
#include <forward_list>
using namespace std;
 
int main (){
  //default version - construct an empty forward_list
  forward_list<int> flist1;
  forward_list<int>::iterator it;

  //fill version - construct a forward_list
  //with 5 elements each equal to 100
  forward_list<int> flist2(5, 100);

  cout<<"flist1 contains: ";
  for(it = flist1.begin(); it != flist1.end(); ++it)
    cout<<*it<<" ";

  cout<<"\nflist2 contains: ";
  for(it = flist2.begin(); it != flist2.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

flist1 contains: 
flist2 contains: 100 100 100 100 100 

Example: Using range and copy version

A forward_list can also be constructed using range or copy version. Consider the following example:

#include <iostream>
#include <forward_list>
using namespace std;
 
int main (){
  forward_list<int> flist1{100, 200, 300};
  forward_list<int>::iterator it;

  //range version - construct flist2 using range
  forward_list<int> flist2(flist1.begin(), flist1.end());

  //copy version - construct flist3 from flist1
  forward_list<int> flist3(flist1); 

  cout<<"flist2 contains: ";
  for(it = flist2.begin(); it != flist2.end(); ++it)
    cout<<*it<<" ";

  cout<<"\nflist3 contains: ";
  for(it = flist3.begin(); it != flist3.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

flist2 contains: 100 200 300
flist3 contains: 100 200 300 

Example: Using move version

Using the move version of forward_list, the content of one forward_list can be moved to another forward_list. Consider the following example:

#include <iostream>
#include <forward_list>
using namespace std;
 
int main (){
  forward_list<int> flist1{10, 20, 30, 40, 50};
  forward_list<int>::iterator it;

  cout<<"flist1 contains: ";
  for(it = flist1.begin(); it != flist1.end(); ++it)
    cout<<*it<<" ";

  //moving all content of flist1 into flist2
  forward_list<int> flist2(move(flist1));

  cout<<"\nflist1 contains: ";
  for(it = flist1.begin(); it != flist1.end(); ++it)
    cout<<*it<<" ";

  cout<<"\nflist2 contains: ";
  for(it = flist2.begin(); it != flist2.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

flist1 contains: 10 20 30 40 50 
flist1 contains: 
flist2 contains: 10 20 30 40 50 

Example: Using initializer list version

The initializer list can also be used to assign values into a forward_list container. Consider the below example:

#include <iostream>
#include <forward_list>
using namespace std;
 
int main (){
  //creating initializer list
  initializer_list<int> ilist = {15, 30, 45, 60, 75};

  //initializer list version - copies all 
  //elements of ilist into the container	
  forward_list<int> flist(ilist);
  forward_list<int>::iterator it;

  cout<<"flist contains: ";
  for(it = flist.begin(); it != flist.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

flist contains: 15 30 45 60 75 

❮ C++ <forward_list> Library