Lesson 29 of 45
C++Lesson 29: List Container
10 min
Course progress64%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 29 of 45
C++10 min
Keep these ideas in mind as you work through the lesson.
std::list implements a doubly‑linked list. It excels at frequent insertions and deletions anywhere in the sequence, with constant‑time complexity.
operator[] is unavailable; you must traverse.splice operation to transfer elements between lists without copying.splice to merge two listsstd::list<std::string> l1 = {\"one\", \"two\"};
std::list<std::string> l2 = {\"three\", \"four\"};
// Move all elements from l2 to the end of l1 without copying
l1.splice(l1.end(), l2);
for (const auto& s : l1) std::cout << s << ' ';
std::cout << '\
';
After splicing, l2 becomes empty, and l1 now contains four elements.
Tip: Use
list::remove_ifwith a predicate lambda to erase elements matching a condition.
<Alert type="info">If you need stable iterators during insertion/erase, std::list is a good choice, though its cache performance is poorer than std::vector.