Results 1 to 2 of 2

Thread: merging QMap

  1. #1
    Join Date
    Jun 2015
    Location
    California, USA
    Posts
    61
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default merging QMap

    How would you merge two (or more) QMaps that do not have conflicting keys/values?

  2. #2
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: merging QMap

    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
    Qt Code:
    1. template <class InputIterator>
    2. void insert (InputIterator first, InputIterator last);
    To copy to clipboard, switch view to plain text mode 
    So you could do
    Qt Code:
    1. firstMap.insert(secondMap.begin(), secondMap.end()
    To copy to clipboard, switch view to plain text mode 
    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

  3. The following user says thank you to ars for this useful post:

    ravas (31st October 2015)

Similar Threads

  1. Need help merging QDomNodes
    By Spoon in forum Qt Programming
    Replies: 3
    Last Post: 29th March 2011, 02:41
  2. Merging files (*.001, *.002 etc.)
    By Altertwin in forum Qt Programming
    Replies: 1
    Last Post: 11th July 2010, 11:15
  3. Merging to different QBrushPattern?
    By ashukla in forum Qt Programming
    Replies: 2
    Last Post: 12th January 2008, 04:32
  4. Menubar Merging
    By elcuco in forum Qt Programming
    Replies: 3
    Last Post: 7th April 2007, 15:54

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.