You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Lists are sequence containers that allow constant-time insertion and
deletion operations anywhere within the sequence, as well as iteration in
both directions.
List containers are implemented as doubly linked lists. These lists can
store their elements in different and unrelated storage locations.
They can be implemented using two pointers in a class or structure,
allowing them to dynamically grow in size.
Adding and removing elements at the beginning or end of a list costs
only constant time, while vectors are generally optimized for
additions and removalsat the end.
Unlike vectors, lists do not support random access, meaning they cannot
be directly accessed at a specific index or have elements retrieved by their
index numbers.
Their header file is <list>.
Declaration Syntax
std::list< Type > list_name;
std::list< Type >* list_ptr;
Initialization Syntax
// Initializer list constructor.
std::list< Type > list_name = { ... };
// Initializer list constructor.
std::list< Type > list_name{ ... };
// No initialization, contain garbage values and behave unpredictably.
std::list< Type > list_name;
// No initialization, contain garbage values and behave unpredictably.
std::list< Type > list_name( size );
// Initialization with the specific value.
std::list< Type > list_name( size, value );
// Initializer list constructor.
std::list< Type > list_name1 = { ... };
// Copy constructor.
std::list< Type > list_name2( list_name1 );
// Initializer list constructor.
std::list< Type > list_name1 = { ... };
// Copy constructor.
std::list< Type > list_name2 = list_name1;
// Initializer list constructor.
std::list< Type > list_name1( { ... } );
// Move constructor.
std::list< Type > list_name2 = std::move( list_name1 );
// Constructs the container with the contents of the range `[first, last)`.
std::list< Type > list_name1( { ... } );
std::list< Type > list_name2( list_name1.begin(), list_name1.end() );
std::list< Type >* list_ptr = new std::list< Type >( size );
std::list< Type >* list_ptr = new std::list< Type >( size, value );
// Initializer list constructor.
std::list< Type > list_name = { ... };
std::list< Type >* list_ptr = new std::list< Type >( list_name );
std::list< Type >* list_ptr = new std::list< Type >{ ... };
(constructor): Constructs the list (public member function).
(destructor): Destructs the list (public member function).
operator=: Assigns values to the container (public member function).
assign: Assigns values to the container (public member function).
assign_range (C++23): Assigns a range of values to the container (public
member function).
get_allocator: Returns the associated allocator (public member function).
front: Access the first element (public member function).
back: Access the last element (public member function).
begin, cbegin: Returns an iterator to the beginning (public member
function).
end, cend: Returns an iterator to the end (public member function).
rbegin, crbegin: Returns a reverse iterator to the beginning (public
member function).
rend, crend: Returns a reverse iterator to the end (public member
function).
empty: Checks whether the container is empty (public member function).
size: Returns the number of elements (public member function).
max_size: Returns the maximum possible number of elements (public member
function).
clear: Clears the contents (public member function).
insert: Inserts elements (public member function).
insert_range (C++23): Inserts a range of elements (public member
function).
emplace: Constructs elements in-place (public member function).
erase: Erases elements and returns a valid iterator (public member
function).
push_back: Adds an element to the end (public member function).
emplace_back: Constructs an element in-place at the end (public member
function).
append_range (C++23): Adds a range of elements to the end (public member
function).
pop_back: Removes the last element (public member function).
push_front: Inserts an element to the beginning (public member function).
emplace_front: Constructs an element in-place at the beginning (public
member function).
prepend_range (C++23): Adds a range of elements to the beginning (public
member function).
pop_front: Removes the first element (public member function).
resize: Changes the number of elements stored (public member function).
swap: Swaps the contents (public member function).
merge: Merges two sorted lists (public member function).
splice: Moves elements from another list (public member function).
remove, remove_if: Removes elements satisfying specific criteria (public
member function).
reverse: Reverses the order of the elements (public member function).
unique: Removes consecutive duplicate elements (public member function).
sort: Sorts the elements (public member function).
Non-member Functions
operator==, operator!=/</<=/>/>= (removed in C++20), operator<=>
(C++20): Lexicographically compares the values of two lists (function
template).
std::swap( std::list ): Specializes the std::swap algorithm (function
template).
erase( std::list ) (C++20), erase_if( std::list ) (C++20): Erases all
elements satisfying specific criteria (function template).
Forward Lists (Singly-linked Lists)
Explanation
Forward lists are sequence containers that allow constant-time
insertion and deletion operations at any point within the sequence.
Forward lists are implemented as singly linked lists.
These lists can store their elements in different, unrelated storage
locations, with order maintained through a link from each element to the next
in the sequence.
They can be implemented using a single pointer within a class or
structure, enabling them to grow dynamically in size.
Unlike vectors, forward lists do not support random access, meaning
elements cannot be accessed directly at specific indices or retrieved by
index numbers.
Their header file is <forward_list>.
Declaration Syntax
std::forward_list< Type > list_name;
std::forward_list< Type >* list_ptr;
Initialization Syntax
// Initializer list constructor.
std::forward_list< Type > list_name = { ... };
// Initializer list constructor.
std::forward_list< Type > list_name{ ... };
// No initialization, contain garbage values and behave unpredictably.
std::forward_list< Type > list_name;
// No initialization, contain garbage values and behave unpredictably.
std::forward_list< Type > list_name( size );
// Initialization with the specific value.
std::forward_list< Type > list_name( size, value );
// Initializer list constructor.
std::forward_list< Type > list_name1 = { ... };
// Copy constructor.
std::forward_list< Type > list_name2( list_name1 );
// Initializer list constructor.
std::forward_list< Type > list_name1 = { ... };
// Copy constructor.
std::forward_list< Type > list_name2 = list_name1;
// Initializer list constructor.
std::forward_list< Type > list_name1( { ... } );
// Move constructor.
std::forward_list< Type > list_name2 = std::move( list_name1 );
// Constructs the container with the contents of the range `[first, last)`.
std::forward_list< Type > list_name1( { ... } );
std::forward_list< Type > list_name2( list_name1.begin(), list_name1.end() );
std::forward_list< Type >* list_ptr = new std::forward_list< Type >( size );
std::forward_list< Type >* list_ptr = new std::forward_list< Type >( size, value );
// Initializer list constructor.
std::forward_list< Type > list_name = { ... };
std::forward_list< Type >* list_ptr = new std::forward_list< Type >( list_name );
std::forward_list< Type >* list_ptr = new std::forward_list< Type >{ ... };
const_iterator: LegacyForwardIterator to const value_type.
Member Functions
(constructor): Constructs the forward_list (public member function).
(destructor): Destructs the forward_list (public member function).
operator=: Assigns values to the container (public member function).
assign: Assigns values to the container (public member function).
assign_range (C++23): Assigns a range of values to the container (public
member function).
get_allocator: Returns the associated allocator (public member function).
front: Access the first element (public member function).
before_begin, cbefore_begin: Returns an iterator to the element before
beginning (public member function).
begin, cbegin: Returns an iterator to the beginning (public member
function).
end, cend: Returns an iterator to the end (public member function).
empty: Checks whether the container is empty (public member function).
max_size: Returns the maximum possible number of elements (public member
function).
clear: Clears the contents (public member function).
insert_after: Inserts elements after an element (public member function).
emplace_after: Constructs elements in-place after an element (public
member function).
insert_range_after (C++23): Inserts a range of elements after an element
(public member function).
erase_after: Erases elements after an element (public member function).
push_front: Inserts an element to the beginning (public member function).
emplace_front: Constructs an element in-place at the beginning (public
member function).
prepend_range (C++23): Adds a range of elements to the beginning (public
member function).
pop_front: Removes the first element (public member function).
resize: Changes the number of elements stored (public member function).
swap: Swaps the contents (public member function).
merge: Merges two sorted lists (public member function).
splice_after: Moves elements from another forward_list (public member
function).
remove, remove_if: Removes elements satisfying specific criteria (public
member function).
reverse: Reverses the order of the elements (public member function).
unique: Removes consecutive duplicate elements (public member function).
sort: Sorts the elements (public member function).
Non-member Functions
operator==, operator!=/</<=/>/>= (removed in C++20), operator<=>
(C++20): Lexicographically compares the values of two forward_lists
(function template).
std::swap( std::forward_list ): Specializes the std::swap algorithm
(function template).
erase( std::forward_list ) (C++20), erase_if( std::list ) (C++20): Erases
all elements satisfying specific criteria (function template).