Can I do this?
theList is a class from me;
actualList the vector name
AFAIK you code is absolutely wrong. What do you want to achieve?Hello.
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.
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
- std::list< std::tuple<QString,short,short> > myList;
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.
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.