Tip:
Highlight text to annotate it
X
Hi and welcome to GeeksforGeeks, in this tutorial we will learn about list in C++.
Lists are sequence containers that allow non-contiguous memory allocation. As compared to vector,
list has slow traversal, but once a position has been found, insertion and deletion are
quick. Normally, when we say a List, we talk about doubly linked list.
Declaring a list is similar to other containers in C++ like this.
We can insert elements at the back and front of a list using push_back and push_front function
like this. Here, I am inserting values in 2 lists, in one list I am inserting at the
back and for the other insertion at front. To print the entire list, we use iterator
from beginning to the end like this. I am using a function to print the entire list
. Let's run this code, we can see that elements
can be inserted at the back and front. Just like push_front and push_back we have
pop_front and pop_back functions to remove elements at the front and back.
Let's run this code, we can see that the output is as expected.
We can use the reverse function to reverse a list, like this. Let's run this code, we
can see that the list is reversed. To sort a list, we use the sort function like
this. Let's run this code, we can see that the list is sorted.
To remove an element from a list we can use remove function and pass the value of the
element to be deleted. Like, here I am deleting 6 from the list like this. To clear the entire
list we use the clear function. To get the size, we use the size function
like this. Let's run this code, we can see that the elements are getting deleted accordingly.
So, this brings us to the end of this video tutorial, thank you for watching, please leave
us your likes and comments in the comments section.