C++ List Library - operator= Function



Description

The C++ function std::list::operator= move the contents of one list into another. It modifies size of the list if necessary.

Declaration

Following is the declaration for std::list::operator= function form std::list header.

C++11

list& operator= (list&& other);

Parameters

other − another list container of same type.

Return value

Returns this pointer.

Exceptions

This member function never throws exception.

Time complexity

Linear i.e. O(n)

Example

The following example shows the usage of std::list::operator= function.

#include <iostream>
#include <list>

using namespace std;

int main(void) {
   list<int> l1 = {1, 2, 3, 4, 5};
   list<int> l2;

   l2 = move(l1);

   cout << "List contains following elements" << endl;

   for (auto it = l2.begin(); it != l2.end(); ++it)
      cout << *it << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

List contains following elements
1
2
3
4
5
list.htm
Advertisements