c++ - STL 绳索 - 何时何地使用

标签 c++ stl ropes

我想知道在什么情况下你会在另一个 STL 容器上使用绳子?

最佳答案

Ropes are a scalable string implementation: they are designed for efficient operation that involve the string as a whole. Operations such as assignment, concatenation, and substring take time that is nearly independent of the length of the string. Unlike C strings, ropes are a reasonable representation for very long strings such as edit buffers or mail messages.

Advantages:

  1. Much faster concatenation and substring operations involving long strings. Inserting a character in the middle of a 10 megabyte rope should take on the order of 10s of microseconds, even if a copy of the original is kept, e.g. as part of an edit history. In contrast, this would take on the order of a second for conventional "flat" string representation. The time required for concatenation can be viewed as constant for most applications. It is perfectly reasonable to use a rope as the representation of a file inside a text editor.

  2. Potentially much better space performance. Minor modifications of a rope can share memory with the original. Ropes are allocated in small chunks, significantly reducing memory fragmentation problems introduced by large blocks

  3. Assignment is simply a (possibly reference counted) pointer assignment. Unlike reference-counted copy-on-write implementations, this remains largely true even if one of the copies is subsequently slightly modified. It is very inexpensive to checkpoint old versions of a string, e.g. in an edit history.

  4. It is possible to view a function producing characters as a rope. Thus a piece of a rope may be a 100MByte file, which is read only when that section of the string is examined. Concatenating a string to the end of such a file does not involve reading the file. (Currently the implementation of this facility is incomplete.)

https://wayback.archive.org/web/20130102093702/https://www.sgi.com/tech/stl/Rope.html

关于c++ - STL 绳索 - 何时何地使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2826431/

相关文章:

c++ - 使用模板类特化消除代码冗余的标准方法是什么?

c++ - 使用 qInstallMessageHandler 的记录器类找不到指向函数的指针?

c++ - std::pair 会破坏其动态分配的对象吗?

c++ - 反向迭代器算法

ruby - 在不转换的情况下将正则表达式与 Ruby 中的非字符串匹配

c++ - 想知道如何将一大块代码放入一个单独的函数中并在它们之间共享变量

c++ - STL priority_queue 的效率

c++ - 为什么 string::find 返回 size_type 而不是迭代器?

c++ - 绳索数据结构和线