C++ List
In C++, a list is a container in the C++ Standard Library that stores elements in a doubly linked list format. It is part of the <list> header and is an important sequence container that provides efficient insertion and deletion of elements at both ends of the container. Unlike vectors or arrays, which store elements in contiguous memory, std::list
stores elements in non-contiguous memory locations. This allows for more flexible and efficient insertion/removal operations, but it comes with the trade-off of slower random access.
Lists are particularly useful when you need to frequently insert or delete elements from anywhere in the container, especially at the beginning or middle, without affecting the overall performance of the program.
std::list
std::list<T> list_name;
T
: The data type of the elements (e.g., int
, float
, std::string
).You can also initialize a list with specific elements:
std::list<T> list_name = {element1, element2, element3};
#include <iostream>
#include <list>
int main() {
// Declare and initialize a list with integers
std::list<int> lst = {1, 2, 3, 4, 5};
// Displaying elements using a range-based for loop
for (int i : lst) {
std::cout << i << " ";
}
return 0;
}
Explanation:
lst
is initialized with the values {1, 2, 3, 4, 5}
.for
loop is used to iterate through the list and print the elements.std::list
std::list
provides several member functions that are useful for manipulating elements within the list.
push_back()
and push_front()
Add elements to the end (push_back()
) or the beginning (push_front()
) of the list.
std::list<int> lst = {1, 2, 3};
lst.push_back(4); // Adds 4 to the end of the list
lst.push_front(0); // Adds 0 to the beginning of the list
pop_back()
and pop_front()
Remove the last element (pop_back()
) or the first element (pop_front()
) from the list.
lst.pop_back(); // Removes 4 from the list
lst.pop_front(); // Removes 0 from the list
size()
Returns the number of elements in the list.
std::cout << "Size of list: " << lst.size() << std::endl;
empty()
Checks if the list is empty.
if (lst.empty()) {
std::cout << "The list is empty!" << std::endl;
} else {
std::cout << "The list is not empty!" << std::endl;
}
clear()
Removes all elements from the list, leaving it empty.
lst.clear(); // Clears the list
insert()
Inserts an element at a specific position in the list.
std::list<int>::iterator it = lst.begin();
lst.insert(it, 10); // Inserts 10 before the first element
erase()
Removes an element at a specific position or range of positions.
lst.erase(it); // Removes the element at position `it`
front()
and back()
Access the first and last element of the list.
std::cout << "First element: " << lst.front() << std::endl; // Output: first element
std::cout << "Last element: " << lst.back() << std::endl; // Output: last element
sort()
Sorts the elements of the list in ascending order (or a custom order if specified).
lst.sort(); // Sorts the list in ascending order
reverse()
Reverses the order of the elements in the list.
lst.reverse(); // Reverses the order of the list
You can iterate through a std::list
using different methods, such as traditional loops, range-based for loops, or iterators.
for
Loop
#include <iostream>
#include <list>
int main() {
std::list<int> lst = {10, 20, 30, 40};
// Traditional for loop using an iterator
for (std::list<int>::iterator it = lst.begin(); it != lst.end(); ++it) {
std::cout << *it << " ";
}
return 0;
}
Example 2: Using Range-Based for
Loop
#include <iostream>
#include <list>
int main() {
std::list<int> lst = {1, 2, 3, 4, 5};
// Range-based for loop
for (int i : lst) {
std::cout << i << " ";
}
return 0;
}
std::list
Over Other Containersstd::list
Use std::list
when: