C++ Standard Library C++ STL Library

C++ <list> - list() Function



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

Syntax

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

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

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

//copy version - copies all elements
//of x into the container	
list (const list& x);
//default version - construct an empty 
//container with no elements
explicit list (const allocator_type& alloc = allocator_type());

//fill version - construct a container 
//with n elements, each equal to val	
explicit list (size_type n);
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>
  list (InputIterator first, InputIterator last,
         const allocator_type& alloc = allocator_type());

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

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

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

//fill version - construct a container 
//with n elements, each equal to val
explicit list (size_type n, const allocator_type& alloc = allocator_type());
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>
  list (InputIterator first, InputIterator last,
         const allocator_type& alloc = allocator_type());

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

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

//initializer list version - copies all 
//elements of il into the container		
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).
x Specify a 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 example below, the list::list function is used to construct a list object.

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

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

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

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

  return 0;
}

The output of the above code will be:

list1 contains: 
list2 contains: 100 100 100 100 100 

Example: using range and copy version

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

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

  //range version - construct list2 using range
  list<int> list2(list1.begin(), list1.end());

  //copy version - construct list3 from list1
  list<int> list3(list1); 

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

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

  return 0;
}

The output of the above code will be:

list2 contains: 100 200 300
list3 contains: 100 200 300 

Example: using move version

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

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

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

  //moving all content of list1 into list2
  list<int> list2(move(list1));

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

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

  return 0;
}

The output of the above code will be:

list1 contains: 10 20 30 40 50 
list1 contains: 
list2 contains: 10 20 30 40 50 

Example: using initializer list version

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

#include <iostream>
#include <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	
  list<int> MyList(ilist);
  list<int>::iterator it;

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

  return 0;
}

The output of the above code will be:

MyList contains: 15 30 45 60 75 

❮ C++ <list> Library