How to insert unique values from QFile into QMap?



  • Hello... This is my first post on the Qt Forums. I have been using Qt for a month and I've really become accustomed to it. Now, I've hit a kind of road block and so decided to ask on the forums. I have a text file,

    one two
    two three
    three four
    four five

    I am able to load the file into memory using QFile. I need to add these values uniquely into a QMap<QString, int> (without repetition). I read the documentation and didn't find anything related to this. How would I do this? Also, how would I read each word pair line by line into a QVector<QPair<QString, QString>>? Is there a method for this also? Please clarify this for me. Thank you.



  • Hi!

    I need to add these values uniquely into a QMap<QString, int>

    I don't understand what that QMap should contain exactly. Do you mean:

    "one" 1
    "two" 2
    "three" 3
    "four" 4
    "five" 5

    ?

    Also, how would I read each word pair line by line into a QVector<QPair<QString, QString>>?

    Like this:

        QFile file("/home/pw/Downloads/myfile.txt");
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
            return -1;
    
        while (!file.atEnd()) {
            const QByteArray line = file.readLine();
            const QString str( line );
            const QStringList lst = str.split(' ');
            if (lst.size() != 2) {
                qDebug() << "error";
                return -2;
            }
            vec << QPair<QString,QString>( lst.first(), lst.last() );
            qDebug() << QString("%1,%2").arg( lst.first() ).arg( lst.last() );
        }
    
        file.close();
    


  • @Wieland said:

    I don't understand what that QMap should contain exactly. Do you mean:

    "one" 1
    "two" 2
    "three" 3
    "four" 4
    "five" 5

    ?

    @Wieland Hi, Wieland. That's exactly what I want to show. The int is actually not important. It's only supposed to show the index of the word after the keys have been sorted in alphabetical order. I will put those in later. The words, however, must come from the file. So, what do I need to do exactly to achieve the above? Please tell me.

    I knew the QString:split() would work but only with QStringList. I thought there might be an alternative where I wouldn't need to initialize a QStringList. I guess not. Thanks for your answer.



  • You need to use QMap::contains() to check for uniqueness:

        QMap<QString,int> mp;
        int c = -1;
    
        QFile file("/home/pw/Downloads/myfile.txt");
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            return -1;
        }
    
        while (!file.atEnd()) {
            const QByteArray line = file.readLine();
            const QString str( line );
            const QStringList lst = str.split(' ');
            if (lst.size() != 2) {
                qDebug() << "error";
                return -2;
            }
            for (int i=0; i<2; i++) {
                if (!mp.contains(lst.at(i))) {
                    mp.insert( lst.at(i), ++c );
                }
            }
    
        }
    
        qDebug() << mp;
    
        file.close();
    


  • @Wieland OK... I think that will work perfectly. Although I see that you have used the same while loop to read the file as the QVector above. However, can this be achieved using readAll() instead of readLine() (just for this QMap, because it need not be read line by line)? In this case, the words are separated by a ' ' and a '\n', so can I modify the split to split(' ' | '\n')? Please clarify this for me. Thanks! :-)



  • split(' ' | '\n')

    That's wrong. Use someting like this:

        const QByteArray all = file.readAll();
        const QStringList lst = QString(all).replace('\n',' ').split(' ');
        for (int i=0; i<lst.size(); i++) {
            if (!mp.contains(lst.at(i))) {
                mp.insert( lst.at(i), ++c );
            }
        }
    

    Hope this helps :-)



  • @Wieland OK... So, you're replacing all the next lines in the string list to spaces and then splitting the strings by spaces. That's pretty neat. Thank you very much. This solution works perfectly for me. I upvoted all your answers. Thank you, Wieland.


Log in to reply
 

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