list operator = in C++ STL

C++Server Side ProgrammingProgramming

Given is the task to show the functionality list operator = function in C++ in STL.

What is List in STL?

List are containers that allow constant time insertion and deletion anywhere in sequence. List are implemented as doubly linked lists. List allow non-contiguous memory allocation. List perform better insertion extraction and moving of element in any position in container than array, vector and deque. In List the direct access to the element is slow and list is similar to forward_list, but forward list objects are single linked lists and they can only be iterated forwards.

What is use of operator = ?

This operator is use to assign new elements to the list by replacing existing element in the list. And it modifies the size of new list according to contents. The another container from which we taking the new element has same data type of first container.

Syntax: listname1 = listname2

Example

Input List1: 50 60 80 90
List2: 90 80 70 60
Output List1: 90 80 70 60
Input List1: E N E R G Y
List2: C A P T I O N
Output List1: C A P T I O N

Approach can be followed

  • First we initialize the two List.

  • Then we use = operator.

  • Then we print new list.

By using the above approach we can assign new element to the list. Working of this operator is just similar to swap( ) function, this operator swap the content of list2 with list1, but it does not swap the content of list1 with list2 and assign new contents to list1.

Example

// C++ code to demonstrate the working of list = operator in STL
#include<iostream.h>
#include<list.h>
Using namespace std;
int main ( ){
   // initializing two lists
   list<int> list1 = { 10, 20, 30, 40, 50 };
   cout<< “ List1: “;
   for( auto x = list1.begin( ); x != list1.end( ); ++x)
      cout<< *x << “ “;
   list<int> list2 = { 40, 50, 60, 70, 80 };
   cout<< “ List2: “;
   for( auto x = list2.begin( ); x != list2.end( ); ++x)
      cout<< *x << “ “;
   list1 = list2;
   // printing new content of list
   cout<< “ New contents of List1 is :”;
   for(auto x = list1.begin( ); x != list1.end( ); ++x)
      cout<< *x<< “ “;
   return 0;
}

Output

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

Input - List1: 10 20 30 40 50
List2: 40 50 60 70 80
Output - New content of List1 is: 40 50 60 70 80

Example

// C++ code to demonstrate the working of list = operator in STL
#include<iostream.h>
#include<list.h>
Using namespace std;
int main ( ){
   // initializing two lists
   list<char> list1 = { 'C', 'H', 'A', 'R', 'G', 'E', 'R' };
   cout<< " List1: ";
   for( auto x = list1.begin( ); x != list1.end( ); ++x)
      cout<< *x << " ";
   List<char> list2 = { 'P', 'O', 'I', 'N', 'T' };
   cout<< " List2: ";
   for( auto x = list2.begin( ); x != list2.end( ); ++x)
   cout<< *x << " ";
   list1 = list2;
   // printing new content of list
   cout<< " New contents of List1 is :";
   for(auto x = list1.begin( ); x != list1.end( ); ++x)
      cout<< *x<< " ";
   return 0;
}

Output

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

Input - List1: C H A R G E R
   List2: P O I N T
Output - New contents of List1 is: P O I N T
raja
Published on 05-Mar-2020 14:12:50
Advertisements