我有这样的代码:
QVector<Point> legalMoves = field.getLegalMoves();
QVector<Cell> costs;
costs.reserve(legalMoves.size());
for (QVector<Point>::iterator i = legalMoves.begin(); i < legalMoves.end(); ++i)
costs.push_back(checkCost(field, *i, player, -100, 100));
Cell cost;
if (player) {
QVector<Point>::iterator m1 = legalMoves.begin();
QVector<Cell>::iterator m2 = costs.begin();
QVector<Cell>::iterator j = costs.begin() + 1;
for (QVector<Point>::iterator i = legalMoves.begin() + 1; i < legalMoves.end(); ++i, ++j)
if (j->status > m2->status) {
m1 = i;
m2 = j;
}
cost=*m2;
}
else {
QVector<Point>::iterator m1 = legalMoves.begin();
QVector<Cell>::iterator m2 = costs.begin();
QVector<Cell>::iterator j = costs.begin() + 1;
for (QVector<Point>::iterator i = legalMoves.begin() + 1; i < legalMoves.end(); ++i, ++j)
if (j->status < m2->status) {
m1 = i;
m2 = j;
}
cost=*m2;
}
QVector<Point> moves;
QVector<Cell>::iterator j = costs.begin();
for (QVector<Point>::iterator i = legalMoves.begin(); i < legalMoves.end(); ++i, ++j)
if (j->status == cost.status)
moves.push_back(*i);
short index = qrand()%moves.size();
return moves[index];
}
当我调试它时,编译器只是在循环内跳过这部分:
if (j->status < m2->status) {
m1 = i;
m2 = j;
}
这意味着函数返回第一个点(或具有相同单元格值而不是最小值/最大值的任何其他点)。为什么会发生这种情况以及如何解决它?