How to Use List in C++: Syntax, Example, and Tips
In C++, you use the
std::list container from the <list> header to store elements in a doubly linked list. You can add, remove, and iterate elements efficiently with functions like push_back(), push_front(), and remove(). Remember to include <list> and use the std namespace or prefix.Syntax
The std::list is a template class that stores elements in a doubly linked list. You declare it with the type of elements it will hold inside angle brackets.
std::list<Type> listName;creates an empty list ofType.- Use
push_back(value)to add an element at the end. - Use
push_front(value)to add an element at the front. - Use
pop_back()orpop_front()to remove elements from ends. - Use iterators or range-based for loops to access elements.
cpp
#include <list> std::list<Type> listName; // Add elements listName.push_back(value); listName.push_front(value); // Remove elements listName.pop_back(); listName.pop_front(); // Iterate for (const auto& item : listName) { // use item }
Example
This example shows how to create a list of integers, add elements to the front and back, remove one element, and print all elements.
cpp
#include <iostream> #include <list> int main() { std::list<int> numbers; // Add elements numbers.push_back(10); // list: 10 numbers.push_front(5); // list: 5, 10 numbers.push_back(15); // list: 5, 10, 15 // Remove element 10 numbers.remove(10); // list: 5, 15 // Print elements for (int num : numbers) { std::cout << num << " "; } std::cout << std::endl; return 0; }
Output
5 15
Common Pitfalls
Some common mistakes when using std::list include:
- Forgetting to include the
<list>header. - Using
std::listwhen astd::vectormight be more efficient for random access. - Trying to access elements by index directly (like
list[0]) which is not supported. - Not using iterators or range-based loops to traverse the list.
Always use iterators or loops to access elements, and choose std::list only when you need fast insertions/removals in the middle of the sequence.
cpp
#include <list> #include <iostream> int main() { std::list<int> myList = {1, 2, 3}; // Wrong: no operator[] for std::list // std::cout << myList[0]; // Error // Right: use iterator or range-based for for (auto it = myList.begin(); it != myList.end(); ++it) { std::cout << *it << " "; } std::cout << std::endl; return 0; }
Output
1 2 3
Quick Reference
| Function | Description |
|---|---|
| push_back(value) | Add element at the end |
| push_front(value) | Add element at the front |
| pop_back() | Remove last element |
| pop_front() | Remove first element |
| remove(value) | Remove all elements equal to value |
| begin() | Return iterator to first element |
| end() | Return iterator past last element |
| size() | Return number of elements |
| empty() | Check if list is empty |
Key Takeaways
Use
std::list from <list> to store elements in a doubly linked list.Add elements with
push_back() or push_front(), and remove with pop_back(), pop_front(), or remove().You cannot access elements by index; use iterators or range-based for loops to traverse the list.
Choose
std::list when you need efficient insertions/removals in the middle, otherwise std::vector may be better.Always include
<list> and use the std namespace or prefix.