C++ list
In this page:
#include <list>
std::list<data_type> list_name = {value1, value2};
list_name.push_back(value);
list_name.push_front(value);
What is std::list?
std::list is a container from the Standard Template Library implemented as a doubly-linked list, where each element is linked to its neighbors, offering fast insertion and removal anywhere in the sequence.
Example: What is std::list?
// Include std::cout and std::cin
#include <iostream>
#include <list>
// Program execution starts in main()
int main() {
std::list<int> nums = {1, 2, 3};
// Print to the console with cout
std::cout << nums.front() << " " << nums.back() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Adding Elements
Elements can be added to either end of a list with push_front and push_back, or inserted anywhere in the middle using an iterator, all in constant time since no shifting of other elements is required.
Example: Adding Elements
// Include std::cout and std::cin
#include <iostream>
#include <list>
// Program execution starts in main()
int main() {
std::list<int> nums = {2, 3};
nums.push_front(1);
nums.push_back(4);
for (int n : nums) std::cout << n << " ";
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Removing Elements
pop_front and pop_back remove the first or last element, while erase removes an element at a given iterator position, all without needing to shift any other elements.
Example: Removing Elements
// Include std::cout and std::cin
#include <iostream>
#include <list>
// Program execution starts in main()
int main() {
std::list<int> nums = {1, 2, 3};
nums.pop_front();
nums.pop_back();
for (int n : nums) std::cout << n << " ";
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Iterating Through a list
A list doesn't support square-bracket indexing since it isn't stored contiguously in memory, so elements are accessed through iterators or a range-based for loop instead.
Example: Iterating Through a list
// Include std::cout and std::cin
#include <iostream>
#include <list>
// Program execution starts in main()
int main() {
std::list<int> nums = {1, 2, 3};
for (int n : nums) std::cout << n << " ";
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
list vs vector
list offers constant-time insertion and removal anywhere, but only sequential access with no indexing, while vector offers fast random access via [] but slower insertion or removal in the middle.
Example: list vs vector
// Include std::cout and std::cin
#include <iostream>
#include <list>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named v (a resizable array)
std::vector<int> v = {1, 2, 3};
// Print to the console with cout
std::cout << v[1] << std::endl;
std::list<int> l = {1, 2, 3};
l.push_front(0);
// Print to the console with cout
std::cout << l.front() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using
[]on astd::list, which does not support random access; use iterators instead. - Calling
std::sort(l.begin(), l.end())on a list, which does not compile; usel.sort(). - Erasing an element while iterating and then using the invalidated iterator.
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: