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. Both ways lookup container
Forum Updated to NodeBB v4.3 + New Features

Both ways lookup container

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 1.3k Views 3 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.
  • Resurr3ctionR Offline
    Resurr3ctionR Offline
    Resurr3ction
    wrote on last edited by
    #1

    Hi, I need to map enum values to text in a container both ways, i.e. values with keys and keys with values. The order does not matter to me. What is the most efficient way of doing this? All Qt containers are said to be efficient only one-way, meaning looking up by key is preferable and looking up keys by values is not. My solution to this so far:

    QHash<MyEnum, QString> values;
    QHash<QString, MyEnum> keys;
    

    Alternatively:

    QList<MyEnum> keys;
    QStringList values;
    
    keys.at(values.indexOf(val)); //to get text from key
    values.at(keys.indexOf(enum)); //to get enum from text value
    

    But maybe there is another solution, ideally with single container... To further explain the text representation of the enum values is translatable and exposed to the user so I cannot use the QMetaEnum for the conversion.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by mcosta
      #2

      I had the same requirement some time ago and I used Boost MultiIndex.

      Just seen on boost web site this

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

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

        The best container for that task will depend on which operations you need it to be fast.
        the boost.bimap suggested by @mcosta is a good starting-point general container that is more or less what your first proposition does.

        If you care most about traversal performance and the bi-directional random lookup is less important (but necessary) then something like QVector<QPair<MyEnum, QString>> with a std::lower_bound for key/value lookup can be a solution.

        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