QString split to QMap



  • 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" }
    }
    

  • Qt Champions 2017

    Why not just use list ?

    QString str ="foo.bar.biz.boz"
    QStringList list1 = str.split('.');
    list1[0] is foo
    list1[1] is bar
    etc...
    


  • 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;
    


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

    Thank you.


  • Qt Champions 2017

    @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. :)



  • @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/


  • Qt Champions 2017

    @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.



  • @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?


  • Qt Champions 2017

    @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. :)



  • @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();
    


  • @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.


  • Qt Champions 2017

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



  • @mrjj You mean QStringList or QMap?


  • Qt Champions 2017

    @Defohin
    QMap for the look up.


  • Qt Champions 2017

    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).



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.