Table of Contents

<list>

<list> is a doubly-linked list with fast insertion and deletion anywhere you have an iterator, but O(n) random access. Use it when you frequently insert/remove in the middle; otherwise prefer std::vector.

Unlike std::forward_list, you can iterate backwards and erase from both ends.

Example

This example demonstrates list insertion at a specific iterator position and iteration over the resulting sequence.

// compile: g++ -std=c++11 -o listexample listexample.cpp
// run: ./listexample
// description: list insertion and iteration
 
#include <list>
#include <iostream>
 
int main() {
    std::list<int> lst = {1, 2, 3};
 
    auto it = lst.begin();
    ++it;
    lst.insert(it, 99);
 
    std::cout << "list: ";
    for (int x : lst) std::cout << x << " ";
    std::cout << "\n";
 
    return 0;
}