The Wayback Machine - https://web.archive.org/web/20240711084733/https://www.geeksforgeeks.org/list-insert-in-c-stl/
Open In App

list insert() in C++ STL

Last Updated : 26 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

CPP




// C++ code to demonstrate the working of
// insert() function
 
#include <iostream>
#include <list> // for list operations
using namespace std;
 
int main()
{
    // declaring list
    list<int> list1;
 
    // using assign() to insert multiple numbers
    // creates 3 occurrences of "2"
    list1.assign(3, 2);
 
    // initializing list iterator to beginning
    list<int>::iterator it = list1.begin();
 
    // iterator to point to 3rd position
    advance(it, 2);
 
    // using insert to insert 1 element at the 3rd position
    // inserts 5 at 3rd position
    list1.insert(it, 5);
 
    // Printing the new list
    cout << "The list after inserting"
         << " 1 element using insert() is : ";
    for (list<int>::iterator i = list1.begin();
         i != list1.end();
         i++)
        cout << *i << " ";
 
    cout << endl;
 
    // using insert to insert
    // 2 element at the 4th position
    // inserts 2 occurrences
    // of 7 at 4th position
    list1.insert(it, 2, 7);
 
    // Printing the new list
    cout << "The list after inserting"
         << " multiple elements "
         << "using insert() is : ";
 
    for (list<int>::iterator i = list1.begin();
         i != list1.end();
         i++)
        cout << *i << " ";
 
    cout << endl;
}


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)



Previous Article
Next Article

Similar Reads

How to insert elements in C++ STL List ?
List has been discussed in many articles, but the sole purpose of this article is to cover all types of insertions that are possible to be carried in a list container and to give a detailed insight on the insertion operations. List and its many functions are defined under the header file "list" . Various list insertions functions are discussed belo
6 min read
emplace vs insert in C++ STL
In C++, all containers (vector, stack, queue, set, map, etc) support both insert and emplace operations. Both are used to add an element in the container.The advantage of emplace is, it does in-place insertion and avoids an unnecessary copy of object. For primitive data types, it does not matter which one we use. But for objects, use of emplace() i
1 min read
multimap insert() in C++ STL
The multimap::insert is a built-in function in C++ STL that is used to insert elements in the multimap container. Syntax: iterator multimap_name.insert({key, element}) Parameters: The function accepts a pair that consists of a key and element which is to be inserted into the multimap container. Return Value: The function returns an iterator pointin
2 min read
map insert() in C++ STL
The map::insert() is a built-in function in C++ STL which is used to insert elements with a particular key in the map container. Syntax: iterator map_name.insert({key, element}) Parameters: The function accepts a pair that consists of a key and element which is to be inserted into the map container. The function does not insert the key and element
4 min read
vector insert() Function in C++ STL
std::vector::insert() is a built-in function in C++ STL that inserts new elements before the element at the specified position, effectively increasing the container size by the number of elements inserted. Time Complexity - Linear, O(N) The insert function is overloaded to work on multiple cases which are as follows: Insert an element at the given
4 min read
multiset insert() function in C++ STL
The multiset::insert() is a built-in function in C++ STL which insert elements in the multiset container or inserts the elements from a position to another position from one multiset to a different multiset. Syntax: iterator multiset_name.insert(element)Time Complexity: O(log n) Since the elements are always in sorted order, the newly inserted elem
4 min read
deque insert() function in C++ STL
The deque::insert() function is a built-in function in C++ which is used to insert elements in the deque. The insert() function can be used in three ways: Extends deque by inserting a new element val at a position.Extends deque by inserting n new element of value val in the deque.Extends deque by inserting new element in the range [first, last). Sy
3 min read
unordered_set insert() function in C++ STL
The unordered_set::insert() is a built-in function in C++ STL which is used to insert a new {element} in the unordered_set container. Each element is inserted only if it is not already present in the container (elements in an unordered_set have unique values). The insertion is done automatically at the position according to the container’s criterio
3 min read
unordered_multiset insert() function in C++ STL
The unordered_multiset::insert() is a built-in function in C++ STL that inserts new elements in the unordered_multiset. This increases the container size. Also notice that elements with the same value are also stored as many times they are inserted. Syntax: Unordered_multiset_name.insert(element) Parameters: This function accepts a single parameter
2 min read
unordered_map insert in C++ STL
The unordered_map::insert() is a built-in function in C++ STL which is used to insert elements with a particular key in the unordered_map container. This function increases container size by 1. This function does not insert duplicate entries. There are following variant of this function. All are overloaded functions. Syntax-1: iterator unordered_ma
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg