Saturday, August 16, 2014

QMap vs. Standard Map (Initializer Lists)

Using enumeration classes will require using an associated array container class (like QMap or QHash) since the size of the enumeration (the number of enumerators) is not obtainable (without kludgy type casting) to dimension an C style array.  As mentioned in the previous post, to use a associated array container class requires run-time assignments to fill the container.

C++11 provides a solution with initialize lists.  Unfortunately, the Qt containers do no support C++11 initializer lists (specifically Qt4 doesn't because Qt5 does contain support for initializer lists).  The Standard Template Library (STL) containers does support initializer lists.  Therefore, the STL containers will be used as needed until the inevitable change to Qt5.  STL containers are technically already available since the various Qt containers have method functions for converting STL containers to and from Qt containers.

For an associated array, either the QMap or std::map container could be used.  Both of these containers order the keys of the elements.  Ordering of the keys is not required in this case.  Qt provides the QHash class for an associated array not requiring ordered keys.  Similarly, STL provides std::unordered_map, which will be the class used.

No comments:

Post a Comment

All comments and feedback welcomed, whether positive or negative.
(Anonymous comments are allowed, but comments with URL links or unrelated comments will be removed.)