The Absence of Tree Containers in the C STL
The C Standard Template Library (STL) doesn't offer any "tree" containers. This omission raises the question: why? And what are suitable alternatives?
Why No Tree Containers in STL?
There are two reasons why one might desire a tree data structure:
1. Hierarchical Object Representation: Modeling a tree-like object hierarchy in the code using a tree structure.
2. Efficient Access Characteristics: Ensuring quick access to elements based on ordering relationships, similar to binary search trees.
Alternatives for Tree Structures
Ordered Associative Containers:
These containers effectively operate as balanced binary trees, guaranteeing efficient logarithmic access times for inserts, deletes, and searches. They also provide additional advantages, such as:
Example:
If one wants to store a hierarchy of employees, with a CEO at the root and multiple levels of subordinates, one can use a std::map
Conclusion
While the C STL doesn't provide tree containers directly, it offers suitable alternatives for both hierarchical representation and efficient access characteristics. Boost's graph library can handle complex graph structures, while ordered associative containers provide tree-like access with a generic and well-established interface.
The above is the detailed content of Why Doesn\'t the C STL Include Tree Containers, and What Are the Alternatives?. For more information, please follow other related articles on the PHP Chinese website!