C++ 基于C+中动态生成的数字的排序列表+;
我有一个对象列表(在本例中为“移动”),我希望根据它们的计算结果进行排序。所以,我有一个列表,和一组与列表中的一个元素“关联”的数字。现在我想对列表元素进行排序,第一个元素的关联编号最低,最后一个元素的关联编号最高。一旦物品被订购,我就可以放弃相关的编号。我该怎么做 这就是我的代码的样子(我们有过):C++ 基于C+中动态生成的数字的排序列表+;,c++,sorting,C++,Sorting,我有一个对象列表(在本例中为“移动”),我希望根据它们的计算结果进行排序。所以,我有一个列表,和一组与列表中的一个元素“关联”的数字。现在我想对列表元素进行排序,第一个元素的关联编号最低,最后一个元素的关联编号最高。一旦物品被订购,我就可以放弃相关的编号。我该怎么做 这就是我的代码的样子(我们有过): list moves=board.getLegalMoves(board.turn); for(i=moves.begin();i!=moves.end();+i) { //... a=max;/
list moves=board.getLegalMoves(board.turn);
for(i=moves.begin();i!=moves.end();+i)
{
//...
a=max;//您可以创建一个比较函数,以您希望的方式比较这两个元素
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;
}
需要注意的是,如果比较函数的计算特别昂贵,那么在排序之前预计算所有相关的秩数可能是值得的
这需要向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;
}
类移动{
//移动类的其余部分
公众:
整数秩;
};
迭代器iter;
for(iter=moves.begin();iter!=moves.end();++iter)
{
//...
(*iter).rank=max;//存储与当前移动相关的数字
}
bool比较等级(常动和第一、常动和第二)
{
如果(first.rank
我建议进行排序。创建一个新的向量(我建议使用向量进行更有效的排序)关联值的对,以及指向其项的指针。对对对向量进行排序,然后从排序后的向量重新生成列表。由于运算符用于对STL集合进行排序。如果只需调用运算符来比较要排序的集合中的元素,则可能需要t按常量Move传递它,而不是复制它。这是通常的方法。但是,我想指出,在向量中排序可能比在列表中排序更有效。当然,除非你的Move类很大,复制它的代价太高。我比较的不是Move的属性,你gh。这只是一些变量。我是否应该将它们作为参数添加到compare_m函数中?即:compare_m(Move first,Move second,int first,int second)通过常量引用传递移动对象,确保所比较的对象是常量method@sbi:const可能是个好主意,所以我在中编辑了它。出于好奇,你能想到在比较函数中使用const没有好处的情况吗?+1,由于随机访问属性,向量排序效率更高他对记忆的关注确实是受欢迎的:)我会使用std::transform
而不是最后一个循环。@pmr,我也喜欢std::transform。我通常也不会在第一个循环中对cost
和ptrToI
使用临时变量。我只是觉得为了说教的目的把所有内容都说出来更有用。
moves.sort(compare_m)
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;
}
#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));
}
std::sort(collection.begin(), collection.end());
list<int> numbers;
numbers.sort();
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;
};
#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's '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;
}