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. QString split to QMap
Qt 6.11 is out! See what's new in the release blog

QString split to QMap

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 4 Posters 6.5k 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.
  • D Defohin

    @mrjj How would I get the key from a QStringList value?

    I wanted the QMap for that:

    https://forum.qt.io/topic/75259/intersection-of-two-qmap/

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #7

    @Defohin
    Well it has indexof to go from String to int but
    the key is the index..

    list1[INDEX]
    so for this string, index is just the KEY

    Of cause , if you have 1,2,3,100,200,400
    then map is ofc better.

    D 2 Replies Last reply
    0
    • mrjjM mrjj

      @Defohin
      Well it has indexof to go from String to int but
      the key is the index..

      list1[INDEX]
      so for this string, index is just the KEY

      Of cause , if you have 1,2,3,100,200,400
      then map is ofc better.

      D Offline
      D Offline
      Defohin
      wrote on last edited by Defohin
      #8

      @mrjj It's not going to have a lot of values, in this case QStringList::indexOf would be better?

      I noticed that indexOf is using QRegularExpression isn't that going to be slower?

      mrjjM 1 Reply Last reply
      0
      • D Defohin

        @mrjj It's not going to have a lot of values, in this case QStringList::indexOf would be better?

        I noticed that indexOf is using QRegularExpression isn't that going to be slower?

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #9

        @Defohin
        Ahh sorry. I missed the intersection thing.
        No, then QMap is better as indexof is slower due to string searching as if the requirement is to
        check if value is there and overwrite or just append if not found.

        But if the number of items is low, then just focus on good names and
        nice structure as performance wise it really wont matter to the app. :)

        D 1 Reply Last reply
        0
        • mrjjM mrjj

          @Defohin
          Well it has indexof to go from String to int but
          the key is the index..

          list1[INDEX]
          so for this string, index is just the KEY

          Of cause , if you have 1,2,3,100,200,400
          then map is ofc better.

          D Offline
          D Offline
          Defohin
          wrote on last edited by
          #10

          @mrjj Based on the other question and this one, I made this:

              QStringList a = { "foo", "bar", "fez", "fiz", "foz", "biz" };
          
              QStringList b = { "foo", "bar", "biz" };
          
              QMap<int, QString> c;
          
              foreach (QString value, b) {
                  if (! a.contains(value))
                      return;
          
                  c.insert(a.indexOf(value), value);
              }
          
              qDebug() << c.keys() << c.values();
          

          It worked, but I don't know if it's going to be faster than:

              QMap<int, QString> a = {
                  { 0, "foo" },
                  { 1, "bar" },
                  { 2, "fez" },
                  { 3, "fiz" },
                  { 4, "foz" },
                  { 5, "biz" }
              };
          
              QStringList b = { "foo", "bar", "biz" };
          
              QMap<int, QString> c;
          
              foreach (QString value, b) {
                  if (! a.values().contains(value)) continue;
                  c.insert(a.key(value), value);
              }
          
              qDebug() << inter.keys() << inter.values();
          
          1 Reply Last reply
          0
          • mrjjM mrjj

            @Defohin
            Ahh sorry. I missed the intersection thing.
            No, then QMap is better as indexof is slower due to string searching as if the requirement is to
            check if value is there and overwrite or just append if not found.

            But if the number of items is low, then just focus on good names and
            nice structure as performance wise it really wont matter to the app. :)

            D Offline
            D Offline
            Defohin
            wrote on last edited by
            #11

            @mrjj said in QString split to QMap:

            @Defohin
            Ahh sorry. I missed the intersection thing.
            No, then QMap is better as indexof is slower due to string searching as if the requirement is to
            check if value is there and overwrite or just append if not found.

            But if the number of items is low, then just focus on good names and
            nice structure as performance wise it really wont matter to the app. :)

            It would have maximum 200 items.

            mrjjM 1 Reply Last reply
            0
            • D Defohin

              @mrjj said in QString split to QMap:

              @Defohin
              Ahh sorry. I missed the intersection thing.
              No, then QMap is better as indexof is slower due to string searching as if the requirement is to
              check if value is there and overwrite or just append if not found.

              But if the number of items is low, then just focus on good names and
              nice structure as performance wise it really wont matter to the app. :)

              It would have maximum 200 items.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #12

              @Defohin
              Well its fine code you have above.
              Seems totally ok for the task.

              D 1 Reply Last reply
              0
              • mrjjM mrjj

                @Defohin
                Well its fine code you have above.
                Seems totally ok for the task.

                D Offline
                D Offline
                Defohin
                wrote on last edited by
                #13

                @mrjj You mean QStringList or QMap?

                mrjjM 1 Reply Last reply
                0
                • D Defohin

                  @mrjj You mean QStringList or QMap?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #14

                  @Defohin
                  QMap for the look up.

                  D 1 Reply Last reply
                  0
                  • D Defohin

                    Hi there, I was wondering, how to transform a QString::split into a QMap<int, QString> where the int is the position of the value and the QString is the value in a QMap?

                    QString identifier = "foo.bar.biz.boz"

                    It shoud be:

                    QMap<int, QString> result = {
                        { 0, "foo" },
                        { 1, "bar" },
                        { 2, "biz" },
                        { 3, "boz" }
                    }
                    
                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #15

                    Why do you need to use a map for this to begin with (you'd always get worst case complexity for this particular case)? Just use a vector (QVector) or a list (QStringList).

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    3
                    • mrjjM mrjj

                      @Defohin
                      QMap for the look up.

                      D Offline
                      D Offline
                      Defohin
                      wrote on last edited by
                      #16

                      @mrjj Thank you, solved the problem and it's working like a boss.

                      1 Reply Last reply
                      1

                      • Login

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