C++ List Library - operator= Function



Description

The C++ function std::list::operator= copies elements from initializer list to list. It modifies the size of the list if necessary.

Declaration

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

C++11

list& operator= (initializer_list<value_type> il);

Parameters

il − Initializer list object.

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) {
   auto it = {1, 2, 3, 4, 5};
   list<int> l;

   l = it;

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

   for (auto it = l.begin(); it != l.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