Reputation: 22810
OK, so here's my issue :
MyClass
), with several variables (one of which is score
)MyClass
objects (e.g. vector<MyClass> MyObjects;
)Now, given that I tried sorting the array (with sort(MyObjects.begin(),MyObjects.end(),MyClassCompare());
) and noticed a considerable drop in performance (and also that some of the elements of the vector may not be needed at all in the end), I'm trying to :
score
value)Is there any way to achieve that using built-in functions/libraries, in C++? Any ideas?
HINT : Speed and performance are crucial.
Upvotes: 0
Views: 211
Reputation: 64068
If you require access to the maximum valued element of a collection, you will have to incur some performance hit either (a) upfront at insertion time, or (b) during searching time. You've noted that (b) is expensive, probably due to the method you chose, and are asking how you can make this quicker.
Out of the box you have priority_queue
which provides probably exactly what you are looking for. I would imagine the performance would be better than your current code.
Upvotes: 1
Reputation: 129344
If you are "collecting" data that you are later going to select things in some order (biggest, smallest, etc), you will have a few choices:
In your case, you are talking of removing some data as from the collection as well. Is it required that you actually remove the data, or that you simply keep track of what you "no longer need"? If the latter, perhaps option four above is a good choice - you simply remove it from the sorted table. Since this is much smaller than the list of items itself [presumably "MyClass" is bigger than two integers].
Upvotes: 0