C++ List is a STL container that stores elements randomly in unrelated locations. To maintain sequential ordering, every list element includes two links:
- one that points to the previous element
- another that points to the next element
In C++, the STL list implements the doubly-linked list data structure. As a result, we can iterate both forward and backward.
Create C++ STL List
To create a list, we need to include the list
header file in our program.
#include<list>
Once we import the header file, we can now declare a list using the following syntax:
std::list<Type> list_name = {value1, value2, ...};
Here,
std::list
- declares an STL container of typelist
<Type>
- the data type of the values to be stored in the listlist_name
- a unique name given to the listvalue1, value2, ...
- values to be stored in the list
Let's see an example,
// create a list of integer type
std::list<int> numbers = {1, 2, 3, 4, 5};
// create a list of character type
std::list<char> vowels = {'a', 'e', 'i', 'o', 'u'};
Note: We can also include list elements without mentioning the assignment operator. For example,
std::list<int> numbers {1, 2, 3, 4, 5};
Example: C++ STL List
#include <iostream>
#include <list>
using namespace std;
int main() {
// create the list
list<int> numbers {1, 2, 3, 4};
// display the elements of the list
cout << "List Elements: ";
for(int number : numbers) {
cout << number <<", ";
}
return 0;
}
Output
List Elements: 1, 2, 3, 4,
In the above example, we have created a list named numbers with elements: 1, 2, 3, 4. We then used a ranged for loop to print the list elements.
Note: We have used list
instead of std::list
because we have already defined std namespace using using namespace std;
.
Basic operations on List
C++ STL provides various functions that we can use to perform different operations on lists. Let's look at some commonly used list functions to perform the following operations:
- Add elements
- Access elements
- Remove elements
1. Add Elements to a List in C++
We can add values in a list using the following functions:
push_front()
- inserts an element to the beginning of the listpush_back()
- adds an element to the end of the list
Let's see an example,
#include <iostream>
#include <list>
using namespace std;
int main() {
// create a list
list<int> numbers = {1, 2, 3};
// display the original list
cout << "Initial List: ";
for(int number: numbers) {
cout << number << ", ";
}
// add element at the beginning
numbers.push_front(0);
// add element at the end
numbers.push_back(4);
// display the modified list
cout << endl << "Final List: ";
for(int number : numbers) {
cout << number << ", ";
}
return 0;
}
Output
Initial List: 1, 2, 3, Final List: 0, 1, 2, 3, 4,
2. Access List Elements
We can access list elements using the following functions:
front()
- returns the first element of the listback()
- returns the last element of the list
Let's see an example,
#include <iostream>
#include <list>
using namespace std;
int main() {
// create a list
list<int> numbers = {1, 2, 3, 4, 5};
// display the first element
cout << "First Element: " << numbers.front() << endl;
// display the last element
cout << "Last Element: " << numbers.back();
return 0;
}
Output
First Element: 1 Last Element: 5
3. Delete List Elements
We can delete list elements using the following functions:
pop_front()
- removes the element at the beginning of the listpop_back()
- removes the element at the end of the list
Here's an example,
#include <iostream>
#include <list>
using namespace std;
int main() {
// create a list
list<int> numbers = {1, 2, 3, 4, 5};
// display the original list
cout << "Inital List: ";
for(int number : numbers) {
cout << number << ", ";
}
// remove the first element of the list
numbers.pop_front();
// remove the last element of the list
numbers.pop_back();
// display the modified list
cout << endl << "Final List: ";
for(int number : numbers) {
cout << number << ", ";
}
return 0;
}
Output
Inital List: 1, 2, 3, 4, 5, Final List: 2, 3, 4,
Other List Functions in C++
While there are many functions that can be used with lists, we will only look at some of the functions in the table below:
Function | Description |
---|---|
reverse() |
Reverses the order of the elements. |
sort() |
Sorts the list elements in a particular order. |
unique() |
Removes consecutive duplicate elements. |
empty() |
Checks whether the list is empty. |
size() |
Returns the number of elements in the list. |
clear() |
Clears all the values from the list |
merge() |
Merges two sorted lists. |
Access elements using an iterator
We can use iterators to access a list element at a specified position. For example,
#include <iostream>
#include <list>
using namespace std;
int main() {
// create a list
list<int> numbers = {1, 2, 3, 4, 5};
// create an iterator to point to the first element of the list
list<int>::iterator itr = numbers.begin();
// increment itr to point to the 2nd element
++itr;
//display the 2nd element
cout << "Second Element: " << *itr << endl;
// increment itr to point to the 4th element
++itr;
++itr;
// display the 4th element
cout << "Fourth Element: " << *itr;
return 0;
}
Output
Second Element: 2 Fourth Element: 4
In the above example,
list<int>::iterator
- defines an iterator for a list ofint
typenumbers.begin()
- sets the iterator to point to the beginning of the list
Notice that we have used ++itr;
repeatedly instead of adding an integer to itr like itr+3;
.
This is because iterators are not simple numeric values like regular integers. They point to specific memory locations in the container.
Incrementing an iterator with the ++
operator makes it point to the next element in the container.
To learn more about iterators, visit C++ STL Iterators.
Frequently Asked Questions
We use the insert()
function to add an element at a specified position.
The syntax for insert()
function for list is:
list_name.insert(iterator, value);
Here,
iterator
- points to the position where the value is to be insertedvalue
- the actual value that needs to be inserted in the position specified by the iterator
Let's see an example,
#include <iostream>
#include <list>
using namespace std;
int main() {
// create a list
list<int> numbers {1, 2, 3};
// display the original list
cout<<"Initial List: ";
for(int number : numbers) {
cout << number << ", ";
}
// create an iterator to point to the 1st position
list<int>::iterator itr = numbers.begin();
// increment the iterator to point to the 3rd position
++itr;
++itr;
// insert 0 at the 3rd position of list
numbers.insert(itr, 0);
// display the modified list
cout<<endl<<"Final List: ";
for(int number : numbers) {
cout << number << ", ";
}
return 0;
}
Output
Initial List: 1, 2, 3, Final List: 1, 2, 0, 3,
We use remove()
function to remove an element at a specified position. The syntax of remove()
function is:
list_name.remove(element);
The remove()
function can be used in the following two ways:
- Using Value
- Using Iterator
1. remove() using Value
#include <iostream>
#include <list>
using namespace std;
int main() {
// create a list
list<int> numbers {1, 2, 1, 3, 4, 1};
// display the original list
cout << "Initial List: ";
for(int number : numbers) {
cout << number << ", ";
}
// remove all the elements with value 1
numbers.remove(1);
// display the modified list
cout << endl << "Final List: ";
for(int number : numbers) {
cout << number << ", ";
}
return 0;
}
Output
Initial List: 1, 2, 1, 3, 4, 1, Final List: 2, 3, 4,
2. remove() using iterator
#include <iostream>
#include <list>
using namespace std;
int main() {
// create a list and an iterator
list<int> numbers {0, 1, 2, 3, 4, 5, 3};
list<int>::iterator itr = numbers.begin();
// display the original list
cout<<"Initial List: ";
for(int number : numbers) {
cout << number << ", ";
}
// point iterator to the 4th element
for (int i = 0; i < 3; i++){
++itr;
}
// remove the 4th element
numbers.remove(*itr);
// display the modified list
cout << endl << "Final List: ";
for(int number : numbers) {
cout << number << ", ";
}
return 0;
}
Output
Initial List: 0, 1, 2, 3, 4, 5, 3, Final List: 0, 1, 2, 4, 5,
Here, both elements with a value of 3 are deleted, even though we only used remove()
function on the fourth element.
This is because the remove()
function removes all the elements having the same value as the element pointed by the iterator.
Also Read