Blog
20 Best FREE Recording Software (2021)
Audio Recording Software are programs designed to record any sound. These applications allow...
In C++, the std::list refers to a storage container. The std:list allows you to insert and remove items from anywhere. The std::list is implemented as a doubly-linked list. This means list data can be accessed bi-directionally and sequentially.
The Standard Template Library list doesn't support fast random access, but it supports sequential access from all directions.
You can scatter list elements in different memory chunks. The information needed for sequential access to data is stored in a container. The std::list can expand and shrink from both ends as needed during runtime. An internal allocator automatically fulfills the storage requirements.
In this C++ tutorial, you will learn:
Here, are reason of using std::List :
To define the std::list, we have to import the <list> header file. Here is the std::list definition syntax:
template < class Type, class Alloc =allocator<T> > class list;
Here is a description of the above parameters:
You can substitute T by any data type, even user-defined types.
This uses the allocator class template by default. It's value-dependent and uses a simple memory allocation model.
#include <algorithm>
#include <iostream>
#include <list>
int main() {
std::list<int> my_list = { 12, 5, 10, 9 };
for (int x : my_list) {
std::cout << x << '\n';
}
}
Output:
Here is a screenshot of the code:
Code Explanation:
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. |
Here is the list of functions provided by the <list> header file:
#include <iostream>
#include <list>
using namespace std;
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 << "Size of list l: " << l.size() << endl;
cout << "List l2 contents: " << endl;
for (auto it = l2.begin(); it != l2.end(); ++it)
cout << *it << endl;
cout << "List l3 contents: " << endl;
for (auto it = l3.begin(); it != l3.end(); ++it)
cout << *it << endl;
return 0;
}
Output:
Here is a screenshot of the code:
Code Explanation:
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. |
There are different functions that we can use to insert values into a list. Let's demonstrate this:
#include <algorithm>
#include <iostream>
#include <list>
int main() {
std::list<int> my_list = { 12, 5, 10, 9 };
my_list.push_front(11);
my_list.push_back(18);
auto it = std::find(my_list.begin(), my_list.end(), 10);
if (it != my_list.end()) {
my_list.insert(it, 21);
}
for (int x : my_list) {
std::cout << x << '\n';
}
}
Output:
Here is a screenshot of the code:
Code Explanation:
It's possible to delete items from a list. The erase() function allows you to delete an item or a range of items from a list.
#include <algorithm>
#include <iostream>
#include <list>
using namespace std;
int main() {
std::list<int> my_list = { 12, 5, 10, 9 };
cout << "List elements before deletion: ";
for (int x : my_list) {
std::cout << x << '\n';
}
list<int>::iterator i = my_list.begin();
my_list.erase(i);
cout << "\nList elements after deletion: ";
for (int x : my_list) {
std::cout << x << '\n';
}
return 0;
}
Output:
Here is screenshot of the code:
Code Explanation:
Audio Recording Software are programs designed to record any sound. These applications allow...
What is the URL? A URL is a global address of documents and protocols to retrieve resource on a...
What is a Software Tester Resume? A software tester resume is a formal document that is mostly one or...
R is a programming language. To use R, we need to install an Integrated Development Environment...
What is TCL? TCL is shell application that reads TCL command from its standard input or from a...
What is the User Experience (UX) design? The user experience (UX) is what a user of a particular product...