Trouble implementing a “rope” data structure in C++

前端 未结 2 1969
一整个雨季
一整个雨季 2021-01-20 05:38

I\'m trying to make a rope data structure. It\'s a type of binary tree, i.e. a recursive data structure.

The purpose of a rope is that splitting and concatenation sh

2条回答
  •  鱼传尺愫
    2021-01-20 06:03

    In practice, though, it involves a heap allocation for (almost?) every modification of the strings.

    If you want to avoid frequent heap allocation performance issues, then I'd suggest using a memory pool for your class that allocates a chunk of memory, and only needs to request a new allocation from the OS when it's full, which should occur pretty infrequently. You can then optimize accesses to the memory pool for allocating small-block data-types like char, etc.

    Andrei Alexandrescu has a great example of a small-block memory allocator in his "Modern C++ Design" book.

提交回复
热议问题