Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. Find index of QVector by comparing an item
QtWS25 Last Chance

Find index of QVector by comparing an item

Scheduled Pinned Locked Moved Brainstorm
12 Posts 5 Posters 14.3k 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.
  • Y Offline
    Y Offline
    Yash
    wrote on last edited by
    #1

    Hi,

    I want to compare a item in a QVector and that will return index.

    Ex. QVector {20,45,57,89} [0,1,2,3]

    for value 46 it should say 2

    for value 21 to 45 it should return 1

    I can do this by using some loop conditions. But I want to know is there any inbuilt function for this.

    http://kineticwing.com : Web IDE, QSS Editor
    http://speedovation.com : Development Lab

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Smar
      wrote on last edited by
      #2

      http://doc.qt.nokia.com/latest/qvector.html#indexOf

      1 Reply Last reply
      0
      • Y Offline
        Y Offline
        Yash
        wrote on last edited by
        #3

        This is not my problem. I know that how to find index of an item .

        My Question is to find index of item which is in range not exactly there.

        Like 46 is nowhere still it returns 2 and not -1 like above func.

        Hope it clears me more.

        http://kineticwing.com : Web IDE, QSS Editor
        http://speedovation.com : Development Lab

        1 Reply Last reply
        0
        • Y Offline
          Y Offline
          Yash
          wrote on last edited by
          #4

          Consider any type of container like QVector or QList any item which can solve my problem.

          http://kineticwing.com : Web IDE, QSS Editor
          http://speedovation.com : Development Lab

          1 Reply Last reply
          0
          • I Offline
            I Offline
            ivan
            wrote on last edited by
            #5

            Since this is applicable only to sorted vectors, it would be strange if there existed a method like that in a QVector.

            You can easily implement it via a binary search.

            Ivan Čukić | ivan.cukic(at)kde.org | KDE e.V.
            Qt Ambassador (from the old Nokia days)

            1 Reply Last reply
            0
            • Y Offline
              Y Offline
              Yash
              wrote on last edited by
              #6

              yea that's what I was thinking i.e binary search but I was looking for any easy solution(s).

              BS is in qt.

              Link is "Binary search in QT":http://doc.trolltech.com/qq/qq15-qalgorithms.html

              http://kineticwing.com : Web IDE, QSS Editor
              http://speedovation.com : Development Lab

              1 Reply Last reply
              0
              • I Offline
                I Offline
                ivan
                wrote on last edited by
                #7

                Well, I don't think you'll be able to use the built-in BS. (you are not looking for an exact match).

                Alternatively, you can go wild :) and implement a Range class and overload some operators... but I think it would be too much hassle for something like this.

                Ivan Čukić | ivan.cukic(at)kde.org | KDE e.V.
                Qt Ambassador (from the old Nokia days)

                1 Reply Last reply
                0
                • Y Offline
                  Y Offline
                  Yash
                  wrote on last edited by
                  #8

                  Agreed

                  I'm doing with core c++.

                  Will post solution here for other guys.

                  http://kineticwing.com : Web IDE, QSS Editor
                  http://speedovation.com : Development Lab

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    tzander
                    wrote on last edited by
                    #9

                    Such a list has been implemented by a troll before and is available here

                    http://qt.gitorious.org/qt-labs/itemviews-ng/blobs/master/src/qsectionspans_p.h

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      janarve
                      wrote on last edited by
                      #10

                      I think what you are looking for is qLowerBound (or qUpperBound). It does a binary search, and returns the position where the item should be inserted, regardless of if the item is found or not.

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        tzander
                        wrote on last edited by
                        #11

                        Just want to add that QMap has upperBound and lowerBound as methods directly; which is a bit nicer to me than using a global method ;)

                        1 Reply Last reply
                        0
                        • Y Offline
                          Y Offline
                          Yash
                          wrote on last edited by
                          #12

                          QMap is perfect solution in this case.

                          I'm little late .... :)

                          Thanks guy!

                          http://kineticwing.com : Web IDE, QSS Editor
                          http://speedovation.com : Development Lab

                          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