A sort list based on dynamically generated numbers in C ++

I have a list of objects ("Move" in this case) that I want to sort based on their estimated score. So, I have a List and many numbers that are “associated” with an item in the list. Now I want to sort the list items with the first item having the smallest number associated and the last with the highest. When the order items, I can drop the associated number. How to do it?

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

list<Move> moves = board.getLegalMoves(board.turn);

for(i = moves.begin(); i != moves.end(); ++i)
{
    //...
    a = max; // <-- number associated with current Move
}
+3
source share
3 answers

Schwartzian transform sort. ( ) . , . operator< std::pair , , .

:

#include <algorithm> // gives you std::sort
#include <utility>   // gives you std::pair

typedef double CostType;
typedef std::pair<CostType, Move*> Pair;

// Create the vector of pairs
std::vector<Pair> tempVec;
tempVec.reserve(moves.size());
for (std::list<Move>::iterator i = moves.begin(); i != moves.end(); ++i)
{
    CostType cost   = calcCost(*i);
    Move*    ptrToI = &(*i);
    tempVec.push_back(Pair(cost, ptrToI));
}

// Now sort 'em
std::sort(tempVec.begin(), tempVec.end());

// Regenerate your original list in sorted order by copying the original
// elements from their pointers in the Pair.
std::list<Move> sortedMoves;
for (std::vector<Pair>::iterator i = tempVec.begin(); i != tempVec.end(); ++i)
{
    sortedMoves.push_back(*(i->second));
}

, calcCost, . , . , N 2 * N * log (N).

+10

, , .

bool compare_m (const Move &first,const Move &second)
{
  if (first.thing_you_are_comparing_on() < second.thing_you_are_comparing_on()) return true;
  else return false;
}

"thing_you_are_comparing_on" - Move, . const , , . compare_m :

moves.sort(compare_m)

, , .

- move, :

class Move{
    //rest of move class

    public:
    int rank;
};

list<Move>::iterator iter;

for(iter = moves.begin(); iter != moves.end(); ++iter)
{
    //...
    (*iter).rank = max; // store the number associated with current Move
}

bool compare_rank (const Move &first,const Move &second)
{
  if (first.rank < second.rank) return true;
  else return false;
}
+4

std::sort STL. , , , operator<, vector, :

std::sort(collection.begin(), collection.end());

vector, a list, , std::sort, std::list:

list<int> numbers;
numbers.sort();

STL sort, STL, . - , , operator< . - "" , operator< functor, . , . sort list, , .

, std::unary_function std::binary_function, , - - . , operator() , :

class compare_functor : public std::binary_function<Move, Move, bool>
{
public:
  bool operator(const Move& lhs, const Move& rhs) const
  {
    int left_val = lhs.Value();
    int right_val = rhs.Value();
    return left_val < right_val;
};

, . Move s, 10 string s. string - 6 . generate_n, generator . , copy , stdout (ostream_iterator). - , strng_val.

, list sort. , sort, evaluator. , , , :

#include <cstdlib>
#include <iostream>
#include <list>
#include <string>
#include <algorithm>
#include <ctime>
#include <sstream>

using namespace std;

class generator 
{
public:
    generator() { srand((unsigned)time(0)); }
    string operator()() const 
    {
        string ret;
        for( int i = 0; i < 6; ++i )
            ret += static_cast<char>((rand()/(RAND_MAX/26)) + 'A');
        return ret;
    }
};

unsigned string_val(const string& rhs)
{
        unsigned val = 0;
        for( string::const_iterator it = rhs.begin(); it != rhs.end(); ++it )
            val += (*it)-'A'+1;
        return val;
};

class evaluator : public std::binary_function<string,string,bool>
{
public:
    bool operator()(const string& lhs, const string& rhs) const
    {
        return string_val(lhs) < string_val(rhs);
    }
};

class string_dumper : public std::unary_function<string, string>
{
public:
    string operator()(const string& rhs) const
    {
        stringstream ss;
        ss << rhs << " = " << string_val(rhs);
        return ss.str();
    }
};

int main() 
{
    // fill a list with strings of 6 random characters
    list<string> strings;
    generate_n(back_inserter(strings), 10, generator());
    // dump it to the screen
    cout << "Unsorted List:\n";
    transform(strings.begin(), strings.end(), ostream_iterator<string>(cout, "\n"), string_dumper());

    // sort the strings according to their numeric values computed by 'evaluator'
    strings.sort(evaluator());  // because this is a 'list', we are using list 'sort'
    // dump it to the screen
    cout << "\n\nSorted List:\n";
    transform(strings.begin(), strings.end(), ostream_iterator<string>(cout, "\n"), string_dumper());
    return 0;

}
0
source

Source: https://habr.com/ru/post/1750125/


All Articles