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. How to insert unique values from QFile into QMap?
QtWS25 Last Chance

How to insert unique values from QFile into QMap?

Scheduled Pinned Locked Moved Solved General and Desktop
qmapqfileqvectorqpair
7 Posts 2 Posters 4.7k 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.
  • WDR_937W Offline
    WDR_937W Offline
    WDR_937
    wrote on last edited by A Former User
    #1

    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.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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();
      
      WDR_937W 1 Reply Last reply
      1
      • ? A Former User

        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();
        
        WDR_937W Offline
        WDR_937W Offline
        WDR_937
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          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();
          
          WDR_937W 1 Reply Last reply
          2
          • ? A Former User

            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();
            
            WDR_937W Offline
            WDR_937W Offline
            WDR_937
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

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

              WDR_937W 1 Reply Last reply
              1
              • ? A Former User

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

                WDR_937W Offline
                WDR_937W Offline
                WDR_937
                wrote on last edited by
                #7

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

                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