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).
#include <iostream>
#include <list> // for list operations
using namespace std;
int main()
{
list< int > list1;
list< int > list2;
list< int > list3;
int arr[10] = { 1, 2, 3, 4 };
list1.assign(4,2);
cout << "The list after inserting multiple elements is : " ;
for (list< int >::iterator i=list1.begin(); i!=list1.end(); i++)
cout << *i << " " ;
cout << endl;
list2.assign(list1.begin(),list1.end());
cout << "The list after copying list elements is : " ;
for (list< int >::iterator i=list2.begin(); i!=list2.end(); i++)
cout << *i << " " ;
cout << endl;
list3.assign(arr,arr+4);
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.
#include <iostream>
#include <list> // for list operations
using namespace std;
int main()
{
list< int > list1;
list1.assign(2,2);
list1.push_front(5);
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;
list1.emplace_front(7);
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.
#include <iostream>
#include <list> // for list operations
using namespace std;
int main()
{
list< int > list1;
list1.assign(2,2);
list1.push_back(5);
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;
list1.emplace_back(7);
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.
#include <iostream>
#include <list> // for list operations
using namespace std;
int main()
{
list< int > list1;
list1.assign(3,2);
list< int >::iterator it = list1.begin();
advance(it,2);
list1.insert(it,5);
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;
list1.insert(it,2,7);
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;
list1.emplace(it,8);
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 write.geeksforgeeks.org or mail your article to review-team@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.