Forward list assign() function in C++ STL

C++Server Side ProgrammingProgramming

Given is the task to show the working of forward_list assign() function in C++.

A forward_list only keeps linkage with the next element unlike normal list that keeps linkage with the next as well as the preceding elements, which helps iterations in forward as well as backward directions. But forward_list can only iterate in the forward direction.

The forward_list::assign() function is a part of the C++ standard template library. It is used to insert elements inside a forward list and if the list already contains some elements then they are replaced by the new ones that are added by the user.

<forward_list> header file should be included to call this function.


There are 3 types of syntax for this function. Each for a different method −

  • Forward_list_name.assign(initializer_list);
  • Forward_list_name.assign(iterator itr1 , iterator itr2);
  • Forward_list_name.assign(count, value);


Initializer_list − In the first case the forward list gets the values that are passed into it as an initialize list in the exact same order.

Iterator − In the second case the forward list gets the values that are included between the values that are being pointed by the iterators itr1 and itr2. This also includes the value pointed by itr1 but not by itr2.

count and value − In the third case the parameter “count” refers to the number of elements to be inserted inside the forward list and “value” refers to the value that has to be assigned to each element.

position, that represents the position at which the new element has to be placed and value, that represents the element that has to be inserted inside the list.

Return Value

The function does not return any value.


Input: assign(5,1)
Output: 1 1 1 1 1

Explanation −

Here we created a forward list “Lt” of type int. Then we used the assign function to assign() values to the list using the parameters count and value.

That assigned the list “Lt” 5 elements each with value 1, which generated the output 1 1 1 1 1.

Approach used in the below program as follows

  • First create three forward lists of type int, let us say “Lt1”, “Lt2” and “Lt3”.
  • Then use the assign() function to assign elements to Lt1 passing an initializer list as the parameter.
  • Then use the assign function to assign elements to Lt2 passing count and value as parameters.
  • Then use the assign function to assign elements to Lt3 passing two iterators as the parameter, let us suppose pointing at the initial and the final positions of the list Lt1 respectively.


Step 1->In function main()
   Declare forward_lists<int> Lt1,Lt2,Lt3
   Call Lt1.assign(initialize list)
   Call Lt2.assign(count,value)
   Call Lt3.assign(L1.begin(),Lt2.end())


 Live Demo

#include <forward_list>
#include <iostream>
using namespace std;
int main() {
   forward_list<int> Lt1;
   forward_list<int> Lt2;
   forward_list<int> Lt3;
   //assigning elements to Lt1
   //assigning elements to Lt2
   //assigning elements to Lt2
   // Display the lists
   cout << "Lt1: ";
   for (int& D : Lt1)
      cout << D << " ";
      cout << endl;
   // Display Lt2
      cout << "Lt2: ";
      for (int& D : Lt2)
         cout << D << " ";
   // Display Lt3
      cout << "Lt3: ";
      for (int& D : Lt3)
         cout << D << " ";
   return 0;


If we run the above code it will generate the following output −

Lt1: 4 9 7 8
Lt2: 6 6 6
Lt3: 4 9 7 8
Published on 20-Jan-2020 07:48:14