Can I do this?
Where:
theList is a class from me;
actualList the vector name
7 replies
Hello. AFAIK you code is absolutely wrong. What do you want to achieve?
Yes I known.
Again. I have a class (“theList”) with 3 parts (QString, short, short).
I have a vector “actualList”. And I want a binarySearch what returns a position. But what I wrote didn’t work [qt-project.org] . And I thought that I have added the QMap [doc.qt.nokia.com] class because it has a searching part.
Please read my previous post [qt-project.org] . :) Thanks.
This is my main problem.
QVector has one template parameter as the data-type that goes into the vector, so the answer is no, you can’t do that code, and i don’t understand what kind of data structure have you tried to create there.
You can just use a QMap [qt-project.org] <short, theList> where the key (the first template parameter) is your procID and the second is the actual instance with that id.
//Wilk, he doesn’t need operator< if he passes a compare function/functor to the binary search
if you want a list of <QString, short, short>, try using a std::list – or QList ;) – of std::tuple’s. Something like
Note that the whitespace between the two “>” is significant, because otherwise your compiler will probably complain about invalid use of operator>>. Also, std::tuple is C++0x, so you may need to update your headers accordingly.
You can then declare this list as extern in your various files and use it as you normally would, as long as the list is defined before any of the externs are actually called at runtime.
@Zlatomir
AFAIK operator< is needed for std::set and QSet, because at least in std::set elements are are stored in an orderly. And search inside std::set and in QSet is actually a binary search, at least in GCC implementation of std::set RB-tree is used. This approach may be a little bit slower but is easy to implement.
You must log in to post a reply. Not a member yet? Register here!


