← Back to C++ Course | Chapter 13: STL Containers & Algorithms | Lesson 3 of 15

C++ list

std::list is a doubly-linked list container from the STL, offering fast insertion and removal at any position, at the cost of slower random access compared to vector.
Syntax
cpp
#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?

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using [] on a std::list, which does not support random access; use iterators instead.
  2. Calling std::sort(l.begin(), l.end()) on a list, which does not compile; use l.sort().
  3. Erasing an element while iterating and then using the invalidated iterator.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.