std::list in C++ with Example
A storage container is what the std::list notation in C++ refers to. You are able to add and remove items from the std:list regardless of where they are located. The std::list is a doubly-linked list in its actual implementation. This indicates that list data can be retrieved in both a sequential and a bidirectional fashion.
The list contained within the Standard Template Library does not provide rapid random access, but it does support sequential access in either direction.
You are able to disperse list elements among various pieces of memory. A container stores the information that must be present in order to achieve sequential access to the data. During runtime, the std::list can be made longer or shorter by adding or removing elements from either end. The needs for storage are satisfactorily satisfied by an internal allocator in the system.
Why use std::list?
When compared to alternative sequence containers, like as array and vector, the std::list performs far better.
They perform more effectively when it comes to inserting, moving, and withdrawing pieces from any position.
The performance of algorithms that carry out similar actions in a laborious manner is likewise improved by the std::list.
List Syntax
Importing the list> header file is required in order for us to define the std::list. The syntax for the definition of std::list is as follows:
template consisting of the classes Type and Alloc = allocator of T, followed by the class list;
The following is an explanation of the aforementioned parameters:
T — Indicates the nature of the component that is present.
You are free to change T to any data type you want, including user-defined types.
Defines the type of the allocator object with the name “alloc.”
The default implementation of this use the allocator class template. It is value-dependent and employs a straightforward approach for memory allocation.
Examples 1:
#include “algorithm” in the file
#include <iostream>
#include ‘list’, then ‘int’, then’main()’, then’std::list’, then’my list’, then ’12, 5, 10, 9′;
Output: for (int x: my list) std::cout x ‘n’;
C++ List Functions
Here are the common std::list functions:
Function | Description |
insert() | This function inserts a new item before the position the iterator points. |
push_back() | This functions add a new item at the list’s end. |
push_front() | It adds a new item at the list’s front. |
pop_front() | It deletes the list’s first item. |
size() | This function determines the number of list elements. |
front() | To determines the list’s first items. |
back() | To determines the list’s last item. |
reverse() | It reverses the list items. |
merge() | It merges two sorted lists. |
The following is a snapshot of the source code:
Code Explanation:
In order to make use of the algorithm’s functions, you must include the header file.
To make use of the features offered by iostream, include its header file.
To make use of the functions offered by the list, include the list header file.
Invoke the method named main(). The logical aspects of the programme should be implemented inside of this function’s main body.
Make a new list and give it the name my list. Fill it with 4 integers.
Create the loop variable x by using a for loop to do so. This variable will be utilised in the process of iterating over the items in the list.
Display on the console the various values contained in the list.
This brings an end to the main body of the for a loop.
This brings an end to the main() function’s body.
<list> Constructors
The following is a list of the functions that are made available by the list header file:
The default function Object() { [native code] } for std::list is called list(), and it is responsible for producing an empty list with no elements.
It builds a list with n entries and assigns a value of zero (0) to each element. The fill function Object() { [native code] } of the std::list::list() function does this.
The range function Object() { [native code] } std::list::list() is responsible for the creation of a list that has many elements in the range of first to last.
It produces a list with a copy of each element that is already contained in the existing list using the copy function Object() { [native code] } of the std::list::list() function.
The move function Object() { [native code] } of the std::list::list() class uses move semantics to build a new list that contains the members of an existing list.
The move semantics are used in the initializer list function Object() { [native code] } std::list::list() to generate a list that contains the members of another list.
Example 2:
#include <iostream>
#include ‘list’ using the standard namespace; int main (void) { list<int> l; list<int> l1 = { 10, 20, 30 }; list<int> l2(l1.begin(), l1.end()); list<int> l3(move(l1)); cout << “The size of the list l is as follows: ” l.size() endl; cout ” List l2 contents: ” endl; for (auto it = l2.begin(); it!= l2.end(); ++it); endl; cout << *it << endl; cout << ” ” List l3 contents: “<< endl; for (auto it = l3.begin(); it != l3.end(); ++it) cout << *it << endl; return 0; }
Output:
The following is a snapshot of the source code:
Code Explanation:
To make use of the features offered by iostream, include its header file.
To make use of the functions offered by the list, include the list header file.
Simply include the std namespace in your code will allow you to use its classes without having to directly call it.
Invoke the method named main(). The logical aspects of the programme should be implemented inside of this function’s main body.
Make a list with the name l that is empty.
Produce a list with the name l1 and fill it with three integers.
Make a new list that you will call l2 and add every item from the list that you will call l1 to it, starting from the beginning.
Utilizing move semantics, a new list with the name l3 should be created. The items on list l3 will be the same ones as are on list l2 after it’s finished.
Show the length of the list denoted by the letter l on the console in addition to the other text.
The console should be updated with some text.
Iterate over the items in the list designated as l2 using an iterator that you have created and given the name it.
The elements of the list that is named l2 should be printed out on the console.
The console should be updated with some text.
Create an iterator that you will refer to as it, and then use it to iterate over the items in the list that you will refer to as l3.
The elements of the list that is labelled l3 should be printed out on the console.
Following the successful execution of the programme, a value must be returned.
This brings an end to the main() function’s body.
Container properties
Here is the list of container properties:
Property | Description |
Sequence | Sequence containers order their elements in a strict linear sequence. Elements are accessed by their position in the sequence. |
Doubly-linked list | Every element has information on how to locate previous and next elements. This allows for constant time for insertion and deletion operations. |
Allocator-aware | An allocator object is used for modifying the storage size dynamically. |
Adding to a List via Insertion
When adding values to a list, we can make use of a variety of functions that are available to us. Let’s put this into practise:
Example 3:
#include “algorithm” in the file
#include <iostream>
#include ‘list’ in the main() function, followed by’std::listint>’ my list is equal to “12, 5”, “10”, and “9”; my list.push front(11); my list.push back(18); auto it = std::find(my list.begin(), my list.end(), 10); if (it does not equal my list.end()), my list.insert(it, 21); for (int x: my list); { std::cout << x << ‘\n’; } }
Output:
The following is a snapshot of the source code:
Code Explanation:
In order to make use of the algorithm’s functions, you must include the header file.
To make use of the functions offered by iostream, include its header file.
To make use of the functions offered by the list, include the list header file.
Invoke the function named main(). The logical aspects of the programme should be implemented inside of this function’s main body.
Make a new list and give it the name my list. Fill it with 4 integers.
Put the item with the identifier 11 at the beginning of the list that is called my list.
Put element 18 at the very end of the list that you’ve been calling my list.
Build an iterator for it, and then use that iterator to search the list my list for the element number 10.
Determine whether or not the aforementioned component was discovered by utilising an if statement.
If the previous element was found, insert element 21 before the element that came before it.
The main body of the if statement has now come to a close.
Create the loop variable x by using a for loop to do so. This variable will be utilised in the process of iterating over the items in the list.
Display on the console the various values contained in the list.
This brings an end to the main body of the for a loop.
This brings an end to the main() function’s body.
Taking Items Off of a List
It is possible to eliminate items from a list using the delete button. You are able to delete a single item or a range of items from a list by utilising the erase() function.
You just need to pass in one integer position in order to delete a single item. The item is going to be removed.
You must provide both the starting and the ending iterators in order to delete a range. Let’s put this into action, shall we?
Example 4:
#include “algorithm” in the file
#include <iostream>
#include <list>
using the standard template namespace std; int main() std::listint> my list = “12, 5”, “10”, and “9”; cout “List elements before deletion: “; for (int x: my list) std::cout x “n”; listint>::iterator I = my list.begin(); my list.erase(i); cout “nList elements after deletion: “; for (int
Output:
A screenshot of the code is as follows:
Code Explanation:
In order to make use of the algorithm’s functions, you must include the header file.
To make use of the functions offered by iostream, include its header file.
To make use of the functions offered by the list, include the list header file.
If we include the std namespace in our programme, we can use its classes without having to call the actual function.
Invoke the function named main(). The logical aspects of the programme should be implemented inside of this function’s main body.
Make a new list and give it the name my list. Fill it with 4 integers.
The console should be updated with some text.
Create the loop variable x by using a for loop to do so. This variable will be utilised in the process of iterating over the items in the list.
Display on the console the various values contained in the list.
This brings an end to the main body of the for loop.
Develop an iterator denoted by the letter I that will point to the initial item in the list.
Apply the erase() function to the iterator that is currently active.
The console should be updated with some text.
Create the loop variable x by using a for loop to do so. This variable will be utilised in the process of iterating over the items in the list.
Display on the console the various values contained in the list. Following the previous deletion is this.
This brings an end to the main body of the for loop.
When the programme is finished running successfully, it must return a value.
This brings an end to the main() function’s body.
Leave a Reply