为什么c++ STL不提供任何“树”容器,用什么代替最好?

我想将对象的层次结构存储为树,而不是使用树来增强性能……


当前回答

这一个看起来很有前途,似乎是你正在寻找的: http://tree.phi-sci.com/

其他回答

The STL's philosophy is that you choose a container based on guarantees and not based on how the container is implemented. For example, your choice of container may be based on a need for fast lookups. For all you care, the container may be implemented as a unidirectional list -- as long as searching is very fast you'd be happy. That's because you're not touching the internals anyhow, you're using iterators or member functions for the access. Your code is not bound to how the container is implemented but to how fast it is, or whether it has a fixed and defined ordering, or whether it is efficient on space, and so on.

因为STL不是一个“万能”库。本质上,它包含了建造东西所需的最小结构。

这一个看起来很有前途,似乎是你正在寻找的: http://tree.phi-sci.com/

所有STL容器在外部都被表示为具有一种迭代机制的“序列”。 树不遵循这个成语。

可能与boost中没有树容器的原因相同。有许多方法可以实现这样的容器,没有一种好方法可以满足所有使用它的人。

需要考虑的一些问题:

节点的子节点数是固定的还是可变的? 每个节点的开销是多少?-即,你需要父指针,兄弟指针等。 提供什么算法?-不同的迭代器,搜索算法等。

最后的问题是,一个对所有人都足够有用的树容器,会因为太过重量级而无法满足大多数使用它的人。如果您正在寻找功能强大的东西,Boost Graph Library本质上是树库的超集。

下面是其他一些通用树的实现:

卡斯珀·彼得斯的树 Adobe的森林 核心:树