C++ List Library - operator= Function



Description

The C++ function std::list::operator= assigns new contents to the list by replacing old ones. It modifies size of the list if necessary.

Declaration

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

C++98

list& operator= (const list& other);

C++11

list& operator= (const list& other);

Parameters

other − another list object 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 = 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