Open In App

Why does the C++ STL not provide any “tree” containers?

What is STL in C++?

The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions. It is a library of container classes, algorithms, functions and iterators. 

It is a generalized library and so, its components are parameterized. Working knowledge of template classes is a prerequisite for working with STL. STL has 4 components:



Containers: Containers are Objects that handle the collection of other objects or elements sequentially or hierarchically implementing well-defined data structures  

Examples: vector, set, queue, stack, list, map, etc.



 

Note: Checkout this article to know more about STL and Containers

Why STL does not have a “tree” container:

First, we should be looking at what is the need for a tree: 

For both of these cases we already have containers 

set, multiset, and unordered_set: 

Basically, the characteristics of these two containers are such that they practically have to be implemented using trees (though this is not actually a requirement).

map, multimap, and unordered_map: 

One of the biggest advantages of these containers is they are generic classes so they can be customized. unordered_map stores data in a random order in O(1) time, and map and multimap store data in an ordered manner in O(log N) time.

Now according to our needs, we can use any of these containers to have the same characteristics as that of a tree.

Related Articles:

Article Tags :