Problem with qBinaryFind() function
-
wrote on 20 Jul 2012, 12:19 last edited by
@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. -
wrote on 22 Jul 2012, 19:03 last edited by
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?
-
wrote on 22 Jul 2012, 19:12 last edited by
[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. -
wrote on 22 Jul 2012, 19:17 last edited by
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 {
//....
}
}
@ -
wrote on 22 Jul 2012, 19:49 last edited by
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)! -
wrote on 22 Jul 2012, 21:11 last edited by
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? -
wrote on 22 Jul 2012, 23:09 last edited by
I think Zlatomir meant (*it)->exist = 1, as you need to dereference the iterator to access the actual value that the iterator points to.
-
wrote on 23 Jul 2012, 04:03 last edited by
moritzg, he has object's into the vector, not pointers, so -> should work fine.
2/9