Lesson 22 of 45 · C++
Lesson 29: List Container
Duration: 10 min
Lesson 22 of 45 · C++
Duration: 10 min
std::list implements a doubly‑linked list. It excels at frequent insertions and deletions anywhere in the sequence, with constant‑time complexity.\n\n---\n\n## Characteristics\n- Non‑contiguous memory layout – each node contains pointers to predecessor and successor.\n- No random access – operator[] is unavailable; you must traverse.\n- Efficient splice operation to transfer elements between lists without copying.\n\n---\n\n## Example: Using splice to merge two lists\ncpp\nstd::list<std::string> l1 = {\"one\", \"two\"};\nstd::list<std::string> l2 = {\"three\", \"four\"};\n// Move all elements from l2 to the end of l1 without copying\nl1.splice(l1.end(), l2);\nfor (const auto& s : l1) std::cout << s << ' ';\nstd::cout << '\\n';\n\n\nAfter splicing, l2 becomes empty, and l1 now contains four elements.\n\n> Tip: Use list::remove_if with a predicate lambda to erase elements matching a condition.\n\n---\n\n<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.