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. indexes of different items in two QStringLists
Qt 6.11 is out! See what's new in the release blog

indexes of different items in two QStringLists

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 958 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.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by
    #1

    i have two QStringLists:

    QStringList l1 = {a1, a2, a3, a4, a5};
    QStringList l2 = {a3, a5};
    

    assume first list is always longer.
    i need the indexes of "extra" items from first list, that aren't in second list.
    in this example i need result to be {0, 1, 3} because items at those indexes in first list, i.e. a1, a2, a4 aren't in second list.

    how can i get those indexes effieciently?

    ODБOïO JonBJ 2 Replies Last reply
    0
    • U user4592357

      i have two QStringLists:

      QStringList l1 = {a1, a2, a3, a4, a5};
      QStringList l2 = {a3, a5};
      

      assume first list is always longer.
      i need the indexes of "extra" items from first list, that aren't in second list.
      in this example i need result to be {0, 1, 3} because items at those indexes in first list, i.e. a1, a2, a4 aren't in second list.

      how can i get those indexes effieciently?

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #2

      @user4592357 hi
      you can iterate over the first QStringList and check if each value is present in de 2nd QStringList with QStringList::contains, if it returns false then you need the index

      U 1 Reply Last reply
      2
      • ODБOïO ODБOï

        @user4592357 hi
        you can iterate over the first QStringList and check if each value is present in de 2nd QStringList with QStringList::contains, if it returns false then you need the index

        U Offline
        U Offline
        user4592357
        wrote on last edited by
        #3

        @LeLev
        yeah i know, but that's almost quadratic time, right?

        JonBJ 1 Reply Last reply
        0
        • U user4592357

          i have two QStringLists:

          QStringList l1 = {a1, a2, a3, a4, a5};
          QStringList l2 = {a3, a5};
          

          assume first list is always longer.
          i need the indexes of "extra" items from first list, that aren't in second list.
          in this example i need result to be {0, 1, 3} because items at those indexes in first list, i.e. a1, a2, a4 aren't in second list.

          how can i get those indexes effieciently?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @user4592357
          You can do it yourself like @LeLev says :)

          I don't think Qt has available methods, but I assume you could convert to std::vector<std::string> and doubtless there are std:: functions for returning the items in one list but not in the other. Whether that's worth it I don't know, do your lists contain 5 or 5,000 items? :) [BTW are the lists sorted?] A C++ std:: algorithms persons here should be able to answer that...? :)

          U 1 Reply Last reply
          1
          • U user4592357

            @LeLev
            yeah i know, but that's almost quadratic time, right?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @user4592357 said in indexes of different items in two QStringLists:

            yeah i know, but that's almost quadratic time, right?

            So you do care about the algorithm! Read my post which has crossed with yours. Are the items sorted?

            1 Reply Last reply
            0
            • JonBJ JonB

              @user4592357
              You can do it yourself like @LeLev says :)

              I don't think Qt has available methods, but I assume you could convert to std::vector<std::string> and doubtless there are std:: functions for returning the items in one list but not in the other. Whether that's worth it I don't know, do your lists contain 5 or 5,000 items? :) [BTW are the lists sorted?] A C++ std:: algorithms persons here should be able to answer that...? :)

              U Offline
              U Offline
              user4592357
              wrote on last edited by user4592357
              #6

              @JonB
              lists aren't sorted, i was thinking of converting them both to sets and subtract and get the items.
              but that's it, it just returns the items, but i need the indexes.

              JonBJ 1 Reply Last reply
              0
              • U user4592357

                @JonB
                lists aren't sorted, i was thinking of converting them both to sets and subtract and get the items.
                but that's it, it just returns the items, but i need the indexes.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @user4592357
                Start here: https://stackoverflow.com/questions/27192967/c-get-the-difference-between-two-vectors

                I started Googling for c++ vector difference, I think those are reasonable key words.

                std::set_difference

                Also, is it important you get the indexes into l1? I suspect stuff out there will return some kind of collection of the different items.

                U 1 Reply Last reply
                3
                • JonBJ JonB

                  @user4592357
                  Start here: https://stackoverflow.com/questions/27192967/c-get-the-difference-between-two-vectors

                  I started Googling for c++ vector difference, I think those are reasonable key words.

                  std::set_difference

                  Also, is it important you get the indexes into l1? I suspect stuff out there will return some kind of collection of the different items.

                  U Offline
                  U Offline
                  user4592357
                  wrote on last edited by
                  #8

                  @JonB
                  yes i need the indexes

                  JonBJ 1 Reply Last reply
                  0
                  • U user4592357

                    @JonB
                    yes i need the indexes

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @user4592357
                    Brainstorm suggestion:

                    1. Put l2 into a hash/map table, for quick lookup.
                    2. Leave l1 unsorted as it is, so you keep indexes.
                    3. Iterate through l1, doing hash lookup in l2-hash.

                    That's much better than quadratic behaviour? That's O(n), plus whatever it takes to create the hash table and do its lookups.

                    1 Reply Last reply
                    4
                    • fcarneyF Offline
                      fcarneyF Offline
                      fcarney
                      wrote on last edited by
                      #10

                      Whatever you do: benchmark and compare methods.

                      C++ is a perfectly valid school of magic.

                      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