Can I do this?
-
[quote author="Wilk" date="1342978778"]Hello.
AFAIK you code is absolutely wrong. What do you want to achieve?[/quote]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":http://qt-project.org/forums/viewthread/18954/ . And I thought that I have added the "QMap":http://doc.qt.nokia.com/4.7-snapshot/qmap.html class because it has a searching part. -
Oh my god.
Implement operator< for your theList
Use QSet or std::set: these classes provide some kinds of search functions. But they don't allow you to change key. If you need it then you'll have to delete an object from container, change key and insert it again.
-
Please read "my previous post":http://qt-project.org/forums/viewthread/18954/ . :) 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":http://qt-project.org/doc/qt-4.8/qmap.html#details <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.
-
@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.