Problem with qBinaryFind() function



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



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



  • 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
    {
    

    //....
    }
    }
    @



  • 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)!



  • 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?



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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.