Details
-
Suggestion
-
Resolution: Unresolved
-
P3: Somewhat important
-
4.7.0
-
None
Description
The QMap class already has the functionality to return a default value if an item isn't found at the specified key. This is found in
const T QMap::value ( const Key & key, const T & defaultValue ) const
This functionality is very useful, but I often find with keyed collections like a map, that I would like to insert an item with some default value at a given key. I usually implement some sort of getDefault() function. This could be defined as such:
/** Gets a value associated with a key from the QMap. If no entry matching the key is found, the default value is inserted and returned * @param key - The key with which to look up the item * @param defaultValue - The default value to insert if the associated key is not found in the map * @return A reference to the value associated with the given key. If there was no value at the key, this will be a reference to the newly added default value */ const T& QMap::getDefault( const Key& key, const T& defaultValue );