Open In App

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 below.

Using assign()

assign() function is used to insert multiple elements in a list in a single operation. “assign()” works in following ways :




// C++ code to demonstrate the working of assign()
  
#include <iostream>
#include <list> // for list operations
using namespace std;
  
int main() 
{
    // declaring list
    list<int> list1;
    list<int> list2;
    list<int> list3;
      
    // initializing array
    int arr[10] = { 1, 2, 3, 4 };
      
    // using assign() to insert multiple numbers
    // creates 4 occurrences of "2"
    list1.assign(4,2);
      
    // Printing the assigned list
    cout << "The list after inserting multiple elements is : ";
    for (list<int>::iterator i=list1.begin(); i!=list1.end(); i++)
       cout << *i << " ";
      
    cout << endl;
      
    // using assign() to copy elements of list to other
    // assigns 4 occurrences of "2"
    list2.assign(list1.begin(),list1.end());
      
    // Printing the assigned list
    cout << "The list after copying list elements is : ";
    for (list<int>::iterator i=list2.begin(); i!=list2.end(); i++)
       cout << *i << " ";
      
    cout << endl;
      
    // using assign() to copy elements of array to list
    // assigns array elements
    list3.assign(arr,arr+4);
      
    // Printing the assigned list
    cout << "The list after copying array elements is : ";
    for (list<int>::iterator i=list3.begin(); i!=list3.end(); i++)
       cout << *i << " ";
      
    cout << endl;
      
}

Output:

The list after inserting multiple elements is : 2 2 2 2 
The list after copying list elements is : 2 2 2 2 
The list after copying array elements is : 1 2 3 4 

Insertion at beginning




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

Output:

The list after inserting elements using push_front is : 5 2 2 
The list after inserting elements using emplace_front is : 7 5 2 2 

Insertion at end




// C++ code to demonstrate the working of 
// push_back() and emplace_back()
  
#include <iostream>
#include <list> // for list operations
using namespace std;
  
int main() 
{
    // declaring list
    list<int> list1;
      
    // using assign() to insert multiple numbers
    // creates 2 occurrences of "2"
    list1.assign(2,2);
      
    // using push_back to insert elements at the end
    // inserts 5 at end
    list1.push_back(5);
      
    // Printing the new list
    cout << "The list after inserting elements using push_back is : ";
    for (list<int>::iterator i=list1.begin(); i!=list1.end(); i++)
       cout << *i << " ";
      
    cout << endl;
      
    // using emplace_back to insert elements at the end
    // inserts 7 at end
    list1.emplace_back(7);
      
    // Printing the new list
    cout << "The list after inserting elements using emplace_back is : ";
    for (list<int>::iterator i=list1.begin(); i!=list1.end(); i++)
       cout << *i << " ";
      
}

Output:

The list after inserting elements using push_back is : 2 2 5 
The list after inserting elements using emplace_back is : 2 2 5 7 

Insertion at any position




// C++ code to demonstrate the working of 
// insert() and emplace()
  
#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;
      
    // using emplace to insert elements at the 6th position
    // inserts 8 at 6th position
    list1.emplace(it,8);
      
    // Printing the new list
    cout << "The list after inserting elements using emplace() is : ";
    for (list<int>::iterator i=list1.begin(); i!=list1.end(); i++)
       cout << *i << " ";
      
}

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 
The list after inserting elements using emplace() is : 2 2 5 7 7 8 2 


Article Tags :