E. Adding items to a list
A list’s components can be added in a variety of ways. Lists are quicker to put elements into than vectors or arrays because each element requires a fixed amount of time. The push back() and push front() methods can be used to insert items into a list. The elements that are to be inserted are passed as an argument to the push back() function, which pushes the element to the back. The push front() function, in contrast, accepts the items to be added as a parameter and pushes the element to the front.
#include <iostream>
#include <list>
using namespace std;
void display(list<int> l)
{
cout << "The elements are: ";
for (auto it : l)
{
cout << it << " ";
}
cout << endl;
}
int main()
{
list<int> l = {1, 2, 3, 4};
display(l);
l.push_back(5);
display(l);
l.push_front(0);
display(l);
}
Output:
The elements are: 1 2 3 4
The elements are: 1 2 3 4 5
The elements are: 0 1 2 3 4 5
We can insert an element at any random point using a different way. The insert() function is employed here. The insert method’s syntax is as follows:
list_name.insert(iterator, element);
The element’s insertion position is shown by the iterator in this case. Here’s an illustration of how to utilise the insert() method:
list<int> l = {1, 2, 3, 4};
display(l);
auto it = l.begin();
it++;
it++;
l.insert(it, 5); //5 is inserted at the third position
display(l);
Output:
The elements are: 1 2 3 4
The elements are: 1 2 5 3 4
F. Getting at/modifying list components
A list’s elements can be changed or accessed, but doing so takes more time. It was unable to easily access any element at any given place using its index number or any other method that was possible for arrays or vectors.
To manually navigate through the list to a certain element or location and dereference the iterator to extract the value at that position, we must utilise iterators.
list<int> l = {1, 2, 3, 4};
list<int>::iterator it = l.begin();
it++;
it++;
cout << "Element at index 2 is " << *it << endl;
Output:
Element at index 2 is 3
G. Eliminating items from a list
There are various methods for eliminating items from a list. A list’s removal is faster than that of vectors or arrays because each element can be removed in a fixed amount of time. It is possible to remove items from a list using the pop back() and pop front() functions. While the pop front() function pops the element from the front, the pop back() function pops the element from the back and requires no parameters.
#include <iostream>
#include <list>
using namespace std;
void display(list<int> l)
{
cout << "The elements are: ";
for (auto it : l)
{
cout << it << " ";
}
cout << endl;
}
int main()
{
list<int> l = {1, 2, 3, 4};
display(l);
l.pop_back();
display(l);
l.pop_front();
display(l);
}
Output:
The elements are: 1 2 3 4
The elements are: 1 2 3
The elements are: 2 3
We can remove an element from any position using a different technique. The erase() function is employed here. When use the erase approach, the syntax is
list_name.erase(iterator);
The element that is wiped in this case is pointed to by the iterator. Here’s an illustration of how to utilise the wipe() method:
list<int> l = {1, 2, 3, 4};
display(l);
auto it = l.begin();
it++;
it++;
l.erase(it); //element at index 2 gets erased
display(l);
Output:
The elements are: 1 2 3 4
The elements are: 1 2 4