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. qBinaryFind for QStringList

qBinaryFind for QStringList

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.3k Views 2 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.
  • saitejS Offline
    saitejS Offline
    saitej
    wrote on last edited by
    #1

    I have 2 QStringLists (L1 is a subset of L2). I need to find the index of all L1 elements in L2. I am using binary search to do the same but I am not sure how to find the index. Also, please do suggest if there is a better way for doing the same.

    Thanks.

     for(int i=0;i<filesGeotagged.length();i++){
    
            QList<QString>::iterator k = qBinaryFind(L2.begin(),L2.end(),L1[i]);
            qDebug() << *k;
    
        }
    
    kshegunovK 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Not tested with Qt but
      Can you do

      int index=(k - L2.begin());
      ?

      You could also use a map, if each L1 element has a good key.
      Then it would be direct lookup.

      saitejS 1 Reply Last reply
      1
      • saitejS saitej

        I have 2 QStringLists (L1 is a subset of L2). I need to find the index of all L1 elements in L2. I am using binary search to do the same but I am not sure how to find the index. Also, please do suggest if there is a better way for doing the same.

        Thanks.

         for(int i=0;i<filesGeotagged.length();i++){
        
                QList<QString>::iterator k = qBinaryFind(L2.begin(),L2.end(),L1[i]);
                qDebug() << *k;
        
            }
        
        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        Your approach is sound. The only drawback is you'd need to sort L2 (before starting the search) for obvious reasons. Another thing you can try if your lists are long is to hash L2. For example:

        // Preparation part:
        QHash<QString, int> indices;
        indices.reserve(L2.size());
        for (qint32 i = 0, size = L2.size(); i < size; i++)
            indices.insert(L2.at(i), i);
        
        // Search part:
        for (qint32 i=0, size = filesGeotagged.length(); i < size; i++)  {
            qDebug() << indices.value(L1.at(i), -1); // Output the index, handle -1 as an error (i.e. put an assertion)
        }
        

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        4
        • mrjjM mrjj

          Hi
          Not tested with Qt but
          Can you do

          int index=(k - L2.begin());
          ?

          You could also use a map, if each L1 element has a good key.
          Then it would be direct lookup.

          saitejS Offline
          saitejS Offline
          saitej
          wrote on last edited by
          #4

          @mrjj

          It works !! Thanks.

          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