VOOZH about

URL: https://www.geeksforgeeks.org/cpp/list-insert-in-c-stl/

⇱ list insert() in C++ STL - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

list insert() in C++ STL

Last Updated : 26 Jun, 2023

The list::insert() is used to insert the elements at any position of list. This function takes 3 elements, position, number of elements to insert and value to insert. If not mentioned, number of elements is default set to 1.

Syntax:

insert(pos_iter, ele_num, ele)

Parameters: This function takes in three parameters:

  • pos_iter: Position in the container where the new elements are inserted.
  • ele_num: Number of elements to insert. Each element is initialized to a copy of val.
  • ele: Value to be copied (or moved) to the inserted elements.

Return Value: This function returns an iterator that points to the first of the newly inserted elements. 


Output
The list after inserting 1 element using insert() is : 2 2 5 2 
The list after inserting multiple elements using insert() is : 2 2 5 7 7 2

Time Complexity - Linear O(N)

Comment
Article Tags: