list insert( ) in C++ STL


Given is the task to show the functionality list insert( ) 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 insert( )

The list insert( ) function is used to insert the elements in the list.

  • The function is use to insert the element at specified position.

  • The function is also use to insert the n No. of element in list.

  • It is also use insert the elements in the range at the specified.

Syntax

insert(iterator position, const value_type& val)
insert(iterator position, size_type n, const value_type& value)
insert(iterator position, iterator first, iterator last)

Parameters

Val − It specifies the new element to be inserted in the list.

Position − It specifies the position in the container where the new element are inserted.

n − Number of element to insert.

First, last − It specifies the iterator which specifying a range of elements to be inserted.

Return value

It returns the iterator that points to the first of the newly inserted elements.

Example

Input List − 50 60 80 90

Output New List − 50 60 70 80 90

Input List − T R E N D

Output New List − T R E N D S

Approach can be followed      

  • First We Declare The List
  • Then we print the List.

  • Then we declare insert( ) function.

By using the above approach we can insert the new element in the list. The new element should have same data type as list.

Example

/ / C++ code to demonstrate the working of list insert( ) function in STL
#include<iostream.h>
#include<list.h>
Using namespace std;
int main ( ){
   List<int> list = { 55, 84, 38, 66, 67 };
   / / print the deque
   cout<< “ List: “;
   for( auto x = List.begin( ); x != List.end( ); ++x)
      cout<< *x << “ “;
   / / declaring insert( ) function
   list.insert( x, 6);
   / / printing new list after inserting new element
   cout<< “New list “;
   for( x=list.begin( ); x != list.end( ); ++x)
      cout<< “ “<<*x;
   return 0;
}

Output

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

Input - List: 55 84 38 66 67
Output - New List: 6 84 38 66 67

Example

/ / C++ code to demonstrate the working of list insert( ) function in STL
#include<iostream.h>
#include<list.h>
Using namespace std;
int main( ){
   List<char> list ={ ‘F’, ‘B’, ‘U’, ‘A’, ‘R’, ‘Y’ };
   cout<< “ List: “;
   for( auto x = list.begin( ); x != list.end( ); ++x)
   cout<< *x << “ “;
   / / declaring insert( ) function
   list.insert(x + 1, 1, ‘E’);
   / / printing new list after inserting new element
   cout<< “ New List:”;
   for( auto x = list.begin( ); x != list.end( ); ++x)
   cout<< “ “ <<*x;
   return 0;
}

Output

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

Input – List: F B U A R Y
Output – New List: F E B U A R Y

Example

/ / C++ code to demonstrate the working of list insert( ) function in STL
#include<iostream.h>
#include<list.h>
#include<vector.h>
Using namespace std;
int main( ){
   list<int> list ={ 10, 44, 34, 98, 15 };
   cout<< “ list: “;
   for( auto x = list.begin( ); x != list.end( ); ++x)
      cout<< *x << “ “;
   vector<int> l(2, 17);
   list.insert(x, l.begin( ), l.end( ) );
   / / printing new list after inserting new element
   cout<< “ New list:”;
   for( auto x = list.begin( ); x != list.end( ); ++x)
      cout<< “ “ <<*x;
   return 0;
}

Output

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

Input – List: 10 44 34 98 15
Output – New list: 17 17 10 44 34 98 15

Updated on: 26-Feb-2020

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements