PDA

View Full Version : merging QMap



ravas
31st October 2015, 19:10
How would you merge two (or more) QMaps that do not have conflicting keys/values?

ars
31st October 2015, 19:58
Hello,

there are several ways:
1. Using only QMap (no std::map as below): Iterate over the second map and add its elements to the first one.
2. Consider using std::map instead of QMap: std::map has insert of the form
template <class InputIterator>
void insert (InputIterator first, InputIterator last);So you could do
firstMap.insert(secondMap.begin(), secondMap.end() See http://www.cplusplus.com/reference/map/map/insert/ for details.
3. Mixing QMap and std::map: Convert both QMaps to std::map and join them as described in 2. Then construct a new QMap from the resulting std::map and assign it to your first QMap.

If you want to use QMap and your maps are small and merge is not called at high frequency, option 1 seems to be the most simple. If you use big maps and/or high frequency merging, think about replacing QMap in favor of std::map. Option 3 above seems to be no good choice due to the conversions to std::map (2 conversions), one conversion back to QMap and the temporary maps created during the merge.

Best regards
ars