Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QMap keys() collection - accessing an item by position

[SOLVED] QMap keys() collection - accessing an item by position

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    frankiefrank
    wrote on last edited by
    #1

    I have a QMap for data that grows - a logging type of scenario.

    For some specific cases, I need to get the key for a certain position (i.e. the 142nd key).

    I tried these options:

    @int keyFromPos = map.keys().at(pos);@

    @
    QMap<int, int>::const_iterator it = map.constBegin(); // int,int for simplicity
    it += pos;
    int keyFromPos = it.key();
    @

    Both ways take longer and longer as the map grows larger. Is there an efficient way to access the key for a specific position?

    "Roads? Where we're going, we don't need roads."

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      With a map I don't think so. It's a structure optimized for fast value lookup not key traversal. If you don't mind the duplication maybe you could store keys separately in a (sorted) vector? There's no better performance than constant time.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        "This":http://qt-project.org/doc/qt-5/containers.html#algorithmic-complexity might be useful

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • F Offline
          F Offline
          frankiefrank
          wrote on last edited by
          #4

          Thanks Chris, SGaist.

          But for option 1, when I access QMap::keys() - I'm no longer using QMap for key traversal - no? I'm looking at a QList<int> where the complexity should be constant time - same time regardless of size.

          This made me suspect that the issue is not with the access to the key at position but rather the generation of the QList<> instance when I call QMap::keys().

          So I checked and indeed, just calling QMap::keys() takes more and more time as the map grows.

          I'll consider having my own sorted keys collection on the side for the fastest access.

          "Roads? Where we're going, we don't need roads."

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Yes, your conclusion is right. keys() does not just return a pre-existing list from the insides of a map. It actually constructs that list in that moment, and constructing that list is not constant time. A map is a (whatever kind) tree structure and to build the list of keys you need to go through each and every node of that tree (following pointers) and copy it out to the list. As I said map is not well suited for this type of operation. While lookup is O(log n) building the list is at least O(n), and it's a quite big O in that case (memory copy one by one and pointer traversal).

            1 Reply Last reply
            0
            • F Offline
              F Offline
              frankiefrank
              wrote on last edited by
              #6

              Thanks for your comment. The results I have for iterating (operation+=) the iterator are acceptable for now.

              "Roads? Where we're going, we don't need roads."

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved