Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Problem with qBinaryFind() function

Problem with qBinaryFind() function

Scheduled Pinned Locked Moved C++ Gurus
9 Posts 3 Posters 4.0k 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.
  • I Offline
    I Offline
    I-sty
    wrote on last edited by
    #1

    @bool myFuncForBinarySearch (const theList &left, const theList & right)
    {
    return (left.procID < right.procID);
    }

    void MainWindow::hasChanged(QString procname, const short pid)
    {
    tmpClass.procID = pid;
    std::vector <theList>::iterator x = qBinaryFind(actualList.begin(), actualList.end(), tmpClass, myFuncForBinarySearch);

    if (x != actualList.end ()) //problem
    {
        //...
    }
    else
    {
        //add process to the actualList
        tmpClass.set_value(procname, pid, 1);
        actualList.push_back(tmpClass);
    }
    

    }@

    I have problem at the line 11.
    The BFind function returns well. It found the element, but at the if where I check that x != the last element (because the func returned the last element that not found the elem).
    So, I'm checking the value of x, but the program go to the line 21 and I don't known why.

    Sorry, for my bad English. My first language is Hungarian.

    1 Reply Last reply
    0
    • ZlatomirZ Offline
      ZlatomirZ Offline
      Zlatomir
      wrote on last edited by
      #2

      I don't see a problem with that code, except that the path it's supposed to go is empty: when the iterator is found there are no instructions to execute so the function returns, so you should tell us what are you trying to achieve?

      https://forum.qt.io/category/41/romanian

      1 Reply Last reply
      0
      • I Offline
        I Offline
        I-sty
        wrote on last edited by
        #3

        [quote author="Zlatomir" date="1342983790"]I don't see a problem with that code, except that the path it's supposed to go is empty: when the iterator is found there are no instructions to execute so the function returns, so you should tell us what are you trying to achieve?[/quote]

        Hi Zlatomir ;)

        I want to find that element position which the procID field is equal to the tmpClass.procID.
        If the element is exist I want to update the this.exist to 1.
        Else I want to add this new element to the List.

        Sorry, for my bad English. My first language is Hungarian.

        1 Reply Last reply
        0
        • ZlatomirZ Offline
          ZlatomirZ Offline
          Zlatomir
          wrote on last edited by
          #4

          Than update that exist to 1 in that if:
          @
          //....
          std::vector <theList>::iterator x = qBinaryFind(actualList.begin(), actualList.end(), tmpClass, myFuncForBinarySearch);

          if (x != actualList.end ()) //problem
          {
              exist = 1; //i didn't really understood what means this.exist: maybe this->exist (this pointer?) 
          }
          else
          {
          

          //....
          }
          }
          @

          https://forum.qt.io/category/41/romanian

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

            Here are two screenshots.

            First: !http://imagerz.com/QEATDEtvAwMAVF1NQwVQ(qt1)!

            On this looks that all is okay. actualList, x, etc.
            But, I press the F10 and, the debugger arrow goes here:
            !http://imagerz.com/QEATDEtvAwMAVF1NQgVQ(qt2)!

            Sorry, for my bad English. My first language is Hungarian.

            1 Reply Last reply
            0
            • ZlatomirZ Offline
              ZlatomirZ Offline
              Zlatomir
              wrote on last edited by
              #6

              You can't do actualList[x].stuff() (x is an iterator not an index) you access the data in the container through that iterator (and you don't actually need the index there):
              @x->exist = 1;@
              And why do you have the extra if there?

              https://forum.qt.io/category/41/romanian

              1 Reply Last reply
              0
              • M Offline
                M Offline
                moritzg
                wrote on last edited by
                #7

                I think Zlatomir meant (*it)->exist = 1, as you need to dereference the iterator to access the actual value that the iterator points to.

                1 Reply Last reply
                0
                • ZlatomirZ Offline
                  ZlatomirZ Offline
                  Zlatomir
                  wrote on last edited by
                  #8

                  moritzg, he has object's into the vector, not pointers, so -> should work fine.

                  https://forum.qt.io/category/41/romanian

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    I-sty
                    wrote on last edited by
                    #9

                    Good Morning! :) Am here.
                    @Zlatomir, the extra if just test.

                    Sorry, for my bad English. My first language is Hungarian.

                    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