How to insert elements in C++ STL List ?

1.7

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 :

  • To insert multiple elements at once in a list.
    syntax : list.assign(number of times, element).
  • To copy elements of 1 list into another.
    syntax : list.assign(lis2.begin(),lis2.end())
  • To copy array elements into list.
    syntax : list.assign(arr,arr+size).
// 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 occurences 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

  • Using push_front() : push_front() is used to insert the element at the beginning of list. Increases list size by 1.
  • Using emplace_front() : Works in a similar way as push_front, but the values are constructed in-place in front position of container, where in push_front, an object is created first, and then copied to the container.
// 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

  • Using push_back() : push_back() is used to insert the element at the end of list. Increases list size by 1.
  • Using emplace_back() : Works in a similar way as push_back, but the values are constructed in-place at back position of container, where in push_back, an object is created first, and then copied to the container.
// 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

  • Using insert(pos_iter,ele_num,ele) : 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.

  • Using emplace(pos_iter,ele) : Works in a similar way as insert(), but the values are constructed in-place in front position of container, where in push_front, an object is created first, and then copied to the container. And only 1 value is allowed to insert at 1 time.
// 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 

This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



1.7 Average Difficulty : 1.7/5.0
Based on 4 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.