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


The C++ forward_list::operator= function is used to assign new content to the container by replacing the current content.

Syntax

//copy version - copies all elements
//of x into the container
forward_list& operator= (const forward_list& x);

//move version - moves elements of x
//into the container
forward_list& operator= (forward_list&& x);

//initializer forward_list version - copies all 
//elements of il into the container
forward_list& operator= (initializer_list<value_type> il);

Parameters

x Specify a forward_list object of same type.
il Specify an initializer_list object.

Return Value

Returns *this.

Time Complexity

Linear i.e, Θ(n).

Example: Using copy version

In the below example, the forward_list::operator= function is used to assign new values to the given forward_list.

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

  //copying all content of f_list1 into f_list2
  forward_list<int> f_list2;
  f_list2 = f_list1;

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

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

  return 0;
}

The output of the above code will be:

f_list1 contains: 10 20 30 40 50
f_list2 contains: 10 20 30 40 50

Example: Using move version

Using the move version of operator=, 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> f_list1{10, 20, 30, 40, 50};
  forward_list<int>::iterator it;

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

  //moving all content of f_list1 into f_list2
  forward_list<int> f_list2;
  f_list2 = move(f_list1);

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

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

  return 0;
}

The output of the above code will be:

f_list1 contains: 10 20 30 40 50
f_list1 contains:
f_list2 contains: 10 20 30 40 50

Example: Using initializer forward_list version

The initializer forward_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 empty forward_list
  forward_list<int> f_list;
  forward_list<int>::iterator it;

  //creating initializer forward_list
  initializer_list<int> ilist = {10, 20, 30, 40, 50};

  //assigning values of f_list using ilist
  f_list = ilist;

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

  return 0;
}

The output of the above code will be:

f_list contains: 10 20 30 40 50

❮ C++ <forward_list> Library