Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-on C++ with Programiz PRO! Claim Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
PRO Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
(https://srv.carbonads.net/ads/click/x/GTND427ECESIT2JWC674YKQUCT
ADS VIA CARBON
(HTTP://CARBONADS.NET/?UTM_SOURCE=WWWPROGRAMIZCOM&UTM_MEDIUM=AD_VIA_LINK&UTM_CAMPAIGN=IN_UNIT&UTM_TERM=CARBON)
C++ List
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
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-on C++ with Programiz PRO! Claim Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
PRO Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
C++ STL list implementation
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,
for printing -our
Thank you std::list declares
contentaatSTL container of type list
www.domain-name.com. Please check back soon for new contents.
<Type> - the data type of the values to be (https://programiz.pro/learn/master-cpp?utm_source=sticky-
stored in the list
36% off Try hands-on C++ with Programiz PRO! Claim Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
Trylist_name - a unique name given to the list
Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
PRO Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
value1, value2, ... - values to be stored in the list
www.domain-name.com
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> {1, 2, 3, 4, 5};
Thank Example:
you for printingC++ STL List
our content at www.domain-name.com. Please check back soon for new contents.
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-on C++ with Programiz PRO! Claim Discount Now
#include <iostream>
banner&utm_campaign=programiz&utm_medium=referral)
#include
Try (https://programiz.pro/learn/master-cpp?utm_source=nav-
<list>
Programiz
PRO Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
using namespace std; www.domain-name.com
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;
Run Code (/cpp-programming/online-compiler)
Output
List Elements: 1, 2, 3, 4,
Thank In
youthe
forabove
printingexample, weathave
our content created a list named
www.domain-name.com. checkwith
numbers
Please backelements: 1, 2,
soon for new contents.
3, 4. We then used a ranged for loop (/cpp-programming/ranged-for-loop) to
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-on C++ with Programiz PRO! Claim Discount Now
print the list elements. banner&utm_campaign=programiz&utm_medium=referral)
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
PRO Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
Note: We have used list instead of std::list because we have already
www.domain-name.com
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:
1. Add elements
2. Access elements
3. 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 list
Thank you push_back() - adds
for printing our anatelement
content to the end of the
www.domain-name.com. list check back soon for new contents.
Please
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Let's
Try hands-on C++see
withan example,
Programiz PRO! Claim Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
PRO Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
#include <iostream>
www.domain-name.com
#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 << ", ";
}
Run Code (/cpp-programming/online-compiler)
Thank Output
you for printing our content at www.domain-name.com. Please check back soon for new contents.
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-onInitial
C++ with Programiz
List: 1,PRO! Claim Discount Now
2, 3, banner&utm_campaign=programiz&utm_medium=referral)
Final List: 0, 1, 2, 3, 4,
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
PRO Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
2. Access List Elements
We can access list elements using the following functions:
front() - returns the first element of the list
back() - returns the last element of the list
Let's see an example,
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
#include <iostream>
#include <list> (https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-on C++ with Programiz PRO! Claim Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
using namespace std;
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
PRO Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
int main() {
www.domain-name.com
// 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;
Run Code (/cpp-programming/online-compiler)
Output
First Element: 1
Last Element: 5
Thank 3.
youDelete
for printingList Elements
our content at www.domain-name.com. Please check back soon for new contents.
WeC++
can delete listPRO!
elements using the (https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-on with Programiz Claim Discount Nowfollowing functions:
banner&utm_campaign=programiz&utm_medium=referral)
Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
Trypop_front() - removes
PRO Search tutorials the element at the beginning of the list
& examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
pop_back() www.domain-name.com
- removes the element at the end of the list
Here's an example,
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
#include <iostream>
#include <list> (https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-on C++ with Programiz PRO! Claim Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
using namespace std;
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
PRO Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
int main() {
www.domain-name.com
// 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;
Run Code (/cpp-programming/online-compiler)
Output
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
Inital List: 1, 2, 3, 4, 5,
Final List: 2, 3, 4,
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-on C++ with Programiz PRO! Claim Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
PRO Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
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.
Thank Access elements
you for printing our contentusing an iterator Please check back soon for new contents.
at www.domain-name.com.
WeC++
can use iterators (https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-on with Programiz PRO!to access
Claim a list
Discount Nowelement at a specified position. For example,
banner&utm_campaign=programiz&utm_medium=referral)
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
PRO Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
#include <iostream>
#include <list> www.domain-name.com
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;
}
Run Code (/cpp-programming/online-compiler)
Thank Output
you for printing our content at www.domain-name.com. Please check back soon for new contents.
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-onSecond
C++ withElement:
Programiz PRO!
2 Claim Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
Fourth Element: 4
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
PRO Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
In the above example, www.domain-name.com
list<int>::iterator - defines an iterator for a list of int type
numbers.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
How to add an element to a list at a specified location?
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
How to remove a list element from a specified location?
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-on C++ with Programiz PRO! Claim Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
PRO Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
Share on: www.domain-name.com
(https://www.facebook.com/sharer/sharer.php? (https://twitter.com/intent/tweet?
u=https://www.programiz.com/cpp- text=Check%20this%20amazing%20article%20on%
programming/list) programming/list)
Did you find this article helpful?
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
36% off
Related Tutorials
Try hands-on C++ with Programiz PRO! Claim Discount Now
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
banner&utm_campaign=programiz&utm_medium=referral)
Try
C++Programiz
(https://programiz.pro/learn/master-cpp?utm_source=nav-
Tutorial Search tutorials & examples C++ Tutorial
PRO
(/) floating&utm_campaign=programiz&utm_medium=referral)
C++ Forward List C++ Vectors
www.domain-name.com
(/cpp-programming/forward-list) (/cpp-programming/vectors)
C++ Tutorial C++ Tutorial
C++ Standard Template Library C++ Iterators
(/cpp-programming/standard-template- (/cpp-programming/iterators)
library)