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
QtWS25 Last Chance

QString split to QMap

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 4 Posters 4.3k Views
  • 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 Offline
    D Offline
    Defohin
    wrote on last edited by
    #1

    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 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Why not just use list ?

      QString str ="foo.bar.biz.boz"
      QStringList list1 = str.split('.');
      list1[0] is foo
      list1[1] is bar
      etc...
      
      1 Reply Last reply
      1
      • M Offline
        M Offline
        mostefa
        wrote on last edited by mostefa
        #3

        you can do something like this:

        ```
        

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

        QStringList stringList = identifier.split(".");
        qDebug() << stringList;
        
        QMap<int,QString> map;
        for(int i=0;i < stringList.size();i++) {
            map.insert(i,stringList.at(i));
        }
        
        qDebug() << map;
        
        D 1 Reply Last reply
        2
        • M mostefa

          you can do something like this:

          ```
          

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

          QStringList stringList = identifier.split(".");
          qDebug() << stringList;
          
          QMap<int,QString> map;
          for(int i=0;i < stringList.size();i++) {
              map.insert(i,stringList.at(i));
          }
          
          qDebug() << map;
          
          D Offline
          D Offline
          Defohin
          wrote on last edited by
          #4

          @mostefa I did exactly as you said, I asked here to see if people would answer in a different way.

          Thank you.

          mrjjM 1 Reply Last reply
          0
          • D Defohin

            @mostefa I did exactly as you said, I asked here to see if people would answer in a different way.

            Thank you.

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

            @Defohin
            But if you use int as key, what would be the benefit for a map over
            QStringList ? It will be slower for the look up most likely. :)

            D 1 Reply Last reply
            0
            • mrjjM mrjj

              @Defohin
              But if you use int as key, what would be the benefit for a map over
              QStringList ? It will be slower for the look up most likely. :)

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

              @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 1 Reply Last reply
              0
              • 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