Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

How to insert elements in C++ STL List?

Last Updated on March 10, 2022 by Ria Pathak

Introduction

The list provides the most efficient ways to get the benefits of a linked list in C++. STL provides the list implementation and features that we can easily exploit to get the advantages of a linked list.

There are multiple ways in which we can insert an element into the list, we will be discussing some of them here.

Approach #1

Using assign() function, to insert multiple elements in the list in one operation only. It is used to insert multiple elements at once in a list.

syntax: list.assign(number of times, element).

Example #1

#include 
#include  // for list operations
using namespace std;
  
int main() 
{
    list list1;  
    list1.assign(4,2);      
    for (list::iterator i=list1.begin(); i!=list1.end(); i++)
       cout << *i << " ";      
    cout << endl;     
}

Output:

2 2 2 2

Approach #2

Insertion at beginning of the list

The next approach is to insert elements at the beginning of the list. This works in the way when it inserts the element in the list, it increases the list size by 1. push_front() function is used to do the insertion at the beginning of the list.

Example #2

#include 
#include  
using namespace std;
  
int main() 
{
    list list1;
      
    list1.assign(2,2);
       list1.push_front(5);

    for (list::iterator i=list1.begin(); i!=list1.end(); i++)
       cout << *i << " ";
}

Output:

5 2 2

Insertion at end of the list

The next approach is to insert elements at the end of the list. This works in the way when it inserts the element in the list, it increases the list size by 1. push_back() function is used to do the insertion at the end of the list.

Example #3

#include 
#include  
using namespace std;
  
int main() 
{
    list list1;
      
    list1.assign(2,2);
       list1.push_back(5);

    for (list::iterator i=list1.begin(); i!=list1.end(); i++)
       cout << *i << " ";
}

Output:

2 2 5

Insertion at any specific in the list

The next approach is to insert elements at any specific position in the list. This works in the way when it inserts the element in the list, it increases the list size by 1. insert() function is used to do the insertion at any specific position in the list. This function insert(pos_iterator, num_elements, element) takes 3 arguments position, number of elements to insert and the element to insert. If number of elements to insert is not mentioned, it is default set to 1.

Example #4

#include 
#include 
using namespace std;
 
int main()
{
    list list1;
      
    list1.assign(5,2);
    list::iterator it = list1.begin();
    advance(it,2);
    list1.insert(it,3);

    for (list::iterator i=list1.begin(); i!=list1.end(); i++){
          cout << *i << " ";
      }
    cout<::iterator i=list1.begin(); i!=list1.end(); i++){
            cout << *i << " ";
      }
}

Output:

2 2 3 2 2 2
2 2 3 4 4 2 2 2

This blog tried to discuss the insertion methods in a list in C++ STL. STL provides more such functionalities and features to insert and use lists in C++. The popular insertion methods are discussed. To practice more such linked list related problems you can check out PrepBytes Linked List.

Leave a Reply

Your email address will not be published. Required fields are marked *