I saw this nice graphic which classifies which STL container would suit based on different requirements of data such as:
-- Fixed Size Vs Variable size
-- Data of same tyme Vs different type
-- Sorted Vs unsorted data
-- Sequential Vs random access
http://plasmahh.projectiwear.org/cce_clean.svg
I notice in that image, that C++ STL there is no container which is
- Variable Size
- Heterogenous (data of different types).
Doesn't C++ have something for this?
PS - There can be many permutations made out the different properties of the containers and many others too might not be provided in STL.
The basic principle in the standard library is that "containers" are homogeneous; the C++ standard doesn't consider things like
std::pair
orstd::tuple
to be containers. (I'd consider the graph misleading, since it does consider them as containers.) If you need a heterogeneous container, you'd have to use a container ofboost::variant
, or something along those lines.std::pair
andstd::tuple
are hardly C++ containers.... so no, there is no heterogeneous containers in the STL, because it's not necessary to have them built-in.There are several approaches to create such containers. The approaches I would recommend are:
For Polymorphism, you can check Boost Pointer Container library.
It mimicks the STL containers, but provides functionalities geared toward polymorphism:
Base&
new_clone
methods)boost::ptr_vector<Base>::iterator it;
,*it
is aBase&
If your types are unrelated, the other possibility is to use Boost Variant. Basically, a variant is similar to:
Of course, since it's boost, it provides specific guarantees to make sure that you can only access the member of the union that is currently active and lifts the restriction on classes with constructors / destructors not being usable in traditional unions.
It also provides facilities, like the
static_visitor
, which is the equivalent of a switch on the type, and will make the compilation error out if one of the possible states is not visited.The fixed size heterogenous containers (like
std::tuple
require the types to be known at compile time. If you want to make a variable sized heterogeneous container, just make astd::vector<std::tuple<T1,T2,...,TN>>
.If you want a heterogeneous container where the types is not known at compile time (whether that would be variable or fixed sized) you'll have to store pointers (or smart pointers) to a base type known at compile time, or alternatively consider something like a container of
boost::any
. The STL doesn't directly provide such a container in either fixed or variable sized with heterogeneous elements determined at run time.A library which is not yet accepted into Boost. But which was proposed for inclusion is targeted toward this :
http://rawgit.com/joaquintides/poly_collection/website/doc/html/index.html
It provides a nice class named any_collection which allows one to have an heterogeneous container via boost::type_erasure::any : http://rawgit.com/joaquintides/poly_collection/website/doc/html/poly_collection/tutorial.html#poly_collection.tutorial.basics.boost_any_collection
Otherwise in C++17 there are simple way to implement this : https://gieseanw.wordpress.com/2017/05/03/a-true-heterogeneous-container-in-c/
Quoting the example of the aforementioned article :
Then usable easily :
The author states it's a toy implementation, but I think this is a really clever way to implement it, and has a simplicity advantage over poly_collection or a vector of variants.
If the element you store would be a
boost::any
orboost::variant
then you can indirectly store heterogeneous data.Well generally C++ Containers are designed to hold objects of a single type using templates. If you want different types that are all derived from one type you can store a container of pointers (I guess you could also have a container of void* to anything...) e.g. std::vector<MyBaseType*>.
If you want completely unrelated types, you can store objects that can safely reference those other types, such as boost::any.
http://www.boost.org/doc/libs/1_47_0/doc/html/any.html
Some examples off the boost site:
boost::variant is similar, but you specify all the allowed types, rather than allowing any type in your container.
http://www.boost.org/doc/libs/1_47_0/doc/html/variant.html