Boost.Intrusive is a library presenting some intrusive containers to the world of C++. Intrusive containers are special containers that offer better performance and exception safety guarantees than non-intrusive containers (like STL containers).
The performance benefits of intrusive containers makes them ideal as a building block to efficiently construct complex containers like multi-index containers or to design high performance code like memory allocation algorithms.
While intrusive containers were and are widely used in C, they became more and more forgotten in C++ due to the presence of the standard containers which don't support intrusive techniques.Boost.Intrusive not only reintroduces this technique to C++, but also encapsulates the implementation in STL-like interfaces. Hence anyone familiar with standard containers can easily use Boost.Intrusive.
The main difference between intrusive containers and non-intrusive containers is that in C++ non-intrusive containers store copies of values passed by the user. Containers use the Allocator template parameter to allocate the stored values:
On the other hand, an intrusive container does not store copies of passed objects, but it stores the objects themselves. The additional data needed to insert the object in the container must be provided by the object itself. For example, to insert MyClass in an intrusive container that implements a linked list, MyClass must contain the needed next and previous pointers:
Semantically, a Boost.Intrusive container is similar to a STL container holding pointers to objects. That is, if you have an intrusive list holding objects of type T, then std::list<T*> would allow you to do quite the same operations (maintaining and navigating a set of objects of type T and types derived from it).
问题 | 介入式 | 非介入式 |
内存管理 | 外部 | 内部,通过分配器 |
插入/删除的时间 | 较快 | 较慢 |
内存位置 | 较好 | 较差 |
可否以值方式保存不可复制和不可移动的对象 | 可以 | 不可 |
异常保证 | 较好 | 较差 |
从值计算迭代器 | 常量复杂度 | 非常量复杂度 |
插入/删除操作的可预见性 | 高 | 低 |
内存占用 | 最少 | 比最少多 |
以值方式插入的对象保持多态行为 | 是 | 不是(切片) |
用户必须修改值的定义以便插入 | 是 | 否 |
容器是可复制的 | 否 | 是 |
被插入对象的生存期由谁管理 | 用户(较复杂) | 容器(较不复杂) |
不使用容器也能破坏容器的不变式 | 较容易 | 较难(只有使用指针容器才会) |
线程安全性分析 | 较难 | 较容易 |
原帖由 coredump 于 14-5-2009 20:41 发表
怎么会觉得奇怪呢,STL的容器就是这样的行为啊。
vector 里就是放的Base的值, 而且只能放Base的值,这是STL Container作为值容器的性质决定的啊,想要在STL容器中体现多态只能用vector这样的方式,或者用Boost P ...
欢迎光临 FreeOZ论坛 (https://www.freeoz.org/ibbs/) | Powered by Discuz! X3.2 |