Results 1 to 4 of 4

Thread: How to return the keys of a map in a vector

  1. #1
    Join Date
    May 2011
    Location
    Dresden, Germany
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to return the keys of a map in a vector

    Hi,

    my method has the following code:

    Qt Code:
    1. const QVector<unsigned int> CLandTypes::getLandTypes()
    2. {
    3. QVector<unsigned int> returnValue;
    4.  
    5. for(QMap<unsigned int, CLandType>::iterator mapLandTypeIterator = (QMap<unsigned int, CLandType>::iterator)d->LandTypes.begin(); mapLandTypeIterator != (QMap<unsigned int, CLandType>::iterator)d->LandTypes.end();++mapLandTypeIterator)
    6. {
    7. returnValue.push_back(mapLandTypeIterator.key());
    8. }
    9. return returnValue;
    10. }
    To copy to clipboard, switch view to plain text mode 

    How can it be done in a better way?

    Rumo

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to return the keys of a map in a vector

    If you really need to have a QVector, then try this:
    Qt Code:
    1. const QVector<unsigned int> CLandTypes::getLandTypes()
    2. {
    3. return d->LandTypes.keys().toVector();
    4. }
    To copy to clipboard, switch view to plain text mode 
    But according to Qt docs:
    For most purposes, QList is the right class to use. Its index-based API is more convenient than QLinkedList's iterator-based API, and it is usually faster than QVector because of the way it stores its items in memory. It also expands to less code in your executable.

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

    Rumo (3rd May 2011)

  4. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to return the keys of a map in a vector

    Just as a side node: With toVector() you won't save so much, because it also expands to a loop. It's only a little bit more sophisticated:
    Qt Code:
    1. Q_OUTOFLINE_TEMPLATE QVector<T> QList<T>::toVector() const
    2. {
    3. QVector<T> result(size());
    4. for (int i = 0; i < size(); ++i)
    5. result[i] = at(i);
    6. return result;
    7. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to return the keys of a map in a vector

    With toVector() you won't save so much
    That's right, it's just to have simpler code, better would be not to use any conversion at all.

Similar Threads

  1. QtableWidget and hot keys
    By sawerset in forum Qt Programming
    Replies: 1
    Last Post: 8th December 2008, 19:29
  2. vector of vector and computes
    By mickey in forum General Programming
    Replies: 1
    Last Post: 15th May 2008, 12:47
  3. Using Short cut keys
    By joseph in forum Qt Programming
    Replies: 14
    Last Post: 18th March 2008, 19:34
  4. insert in a vector of vector
    By mickey in forum General Programming
    Replies: 3
    Last Post: 6th March 2007, 08:45
  5. vector of vector size
    By mickey in forum General Programming
    Replies: 5
    Last Post: 13th February 2007, 15:59

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.