Efficient linked list in C++?

后端 未结 11 734
孤街浪徒
孤街浪徒 2021-02-02 06:36

This document says std::list is inefficient:

std::list is an extremely inefficient class that is rarely useful. It performs a heap allocation

11条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-02 07:09

    As an alternative, you can use a growable array and handle the links explicitly, as indexes into the array.

    Unused array elements are put in a linked list using one of the links. When an element is deleted, it is returned to the free list. When the free list is exhausted, grow the array and use the next element.

    For the new free elements, you have two options:

    • append them to the free list at once,
    • append them on demand, based on the number of elements in the free list vs. the array size.

提交回复
热议问题