sorting - Sort List based on dynamically generated numbers in C++ -


I have a list of objects (in this case "move") that I sort by their calculation evaluation Want to So, I have a list, and a bunch of numbers that are "connected" with one element in the list. Now I want to be the topmost and most lastest person with the first element of the list element with the least connected number. Once the item has been ordered, I can leave the related number. How do i do this

This is my code that looks like (kind've):

  list & lt; Move & gt; Moves = board.gatelegalmows (board.turn); (I = moves.begin (); i! = Moves.end (); ++ i) {// ... a = max; // & lt; - Number associated with the current motion}  

I would suggest a type. A new vector of the associated value pairs (I suggest to give a vector for more efficient sorting) and make an indicator for its item. Sort the pairs of vectors and then recreate the list from the sorted vector. Since operator & lt; is defined on std :: pair , compared with the first item of the pair, and secondly, you will get the proper order.

Example:

#include & lt; Algorithm & gt; // You get std :: sort #include & lt; Utility & gt; // gives you std :: pair typedef double costType; Typedef std :: pair & lt; CostType, Move * & gt; pair; // Creating Vector of Joints std :: vector & lt; Pair & gt; TempVec; TempVec.reserve (moves.size ()); (Std :: list & lt; move & gt; :: for iterator i = moves.begin (); i! = Moves.end (); ++ i) Cost type cost = calcCost (* i); Transfer * ptrToI = & amp; (* I); TempVec.push_back (pair (cost, ptrToI)); } // Now sort 'std :: sort (tempVec.begin (), tempVec.end ()); // duplicate the original // elements from your pointers in pairs, and recreate your original list in the sorted order. Std :: from the list & lt; Move & gt; SortedMoves; (Std :: vector & gt; pair & gt; :: iterator i = tempVec.begin (); i! = TempVec.end (); ++ i) {sortedMoves.push_back (* (i- & gt; second) ) for; }

Note that you will need the calcocost function which I have assumed here if your comparison value calculation is time consuming, this approach is a comparative function There is an advantage on making In this way, you only pay the cost to calculate N times instead of 2 * N * log (n).


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -