Javatpoint Logo
Javatpoint Logo

C++ List insert()

C++ List insert() function inserts a new element just before the specified position. It increases the size of the list container by the number of elements added in the list.

Syntax

Parameter

pos: It defines the position before which the new element is to be inserted.

value: The value to be inserted.

n: Number of times the value to be occurred.

( first,last): It defines the range of the elements to be inserted at position pos.

Return value

It returns an iterator pointing to the newly constructed element.

Example 1

Let's see a simple example

Output:

51234

In this example, iterator points to the first element of the list. Therefore, 5 is inserted before the first element of the list using insert() function.

Example 2

Let's see a simple example when n is given.

Output:

java java C is a language 

In this example, insert() function inserts the string "java" 2 times before the first element of the list.

Example 3

Let's see a simple example

Output:

6 7 8 9 1 2 3 4 5

In this example, range(first, last) of list li1 is given. Therefore, the insert() function inserts the elements between this range in the list li.





Please Share

facebook twitter google plus pinterest