【问题标题】:trying to sort a vector of objects with a comparison operators尝试使用比较运算符对对象向量进行排序
【发布时间】:2020-02-22 06:09:23
【问题描述】:

我正在尝试实现纸牌游戏,并且正在使用纸牌矢量。 我实现了每一个比较运算符,希望“排序”函数会根据这些运算符进行排序,但我总是认为 ace 是最小的牌。

我尝试手动排序,但它变得非常丑陋。

class Card {

    int worth;
    int num;
    Shapes shape_id;
    string name;
    string shape_name;
    string color;
    char symbol;
    string intToShape(int n);
    string intToColor(int n);
public:
    Card(int n, int shap);
    Card();
    int getNumOfCard() const;
    int getShapeOfCard() const;
    friend std::ostream& operator<<(std::ostream& os, const Card& c);
    bool operator >(Card& other);
    bool operator <(Card& other);
    bool operator ==(Card& other);
    bool operator >=(Card& other);
    bool operator <=(Card& other);
    bool operator !=(Card& other);
};

卡片的主人:

Card::Card(int n, int shap) : num(n) , shape_id((Shapes)shap) {
    if (num>10 || num ==1) {
        switch (num) {
            case 11:
                name = "Jack" + intToShape(shap);
                symbol ='J';
                break;
            case 12:
                name = "Quin" + intToShape(shap);
                symbol = 'Q';
                break;
            case 13:
                name = "King" + intToShape(shap);
                symbol = 'K';
                break;
            case 1:
                name = "Ace" + intToShape(shap);
                symbol = 'A';
                break;
            default:
                string exceptionMessage("num > 13"); 
                throw (PckErr(exceptionMessage));
        }
    } else {
        symbol = 'N';
        name = std::to_string(num) + intToShape(shap);
    }
    if (num == 1) {
        worth = 14;  //ace worth 14!
    } else {
        worth = num;
    }
    shape = intToShape(shap);
    color = intToColor(shap);
}

排序部分:

for (int i = 0; i < 12; ++i) {
    histogram[pCard[i]->getNumOfCard()]++;
    it = sorted_hand.insert(it,pCard[i]);
}
std::sort(sorted_hand.begin(), sorted_hand.end());

运营商实现:

bool Card::operator>(Card &other) {
    return this->worth > other.worth;
}

bool Card::operator<(Card &other) {
    return this->worth < other.worth;
}

... same for all

我希望 5 张卡片的排序向量是: 2,3,4,5,1 但实际的向量是: 1,2,3,4,5。

【问题讨论】:

  • 你为什么期待2,3,4,5,1?看起来没有排序
  • 如果按运营商排序
  • 尝试创建一个比较函数,如果该值小于另一个值,则返回该函数,并将其添加为 std::sort 的最后一个元素。 std::sort(sorted_hand.begin(), sorted_hand.end(), compareFunction);
  • @GuySadoun 为什么?你在你的operator &lt; 中做this-&gt;worth &lt; other.worth 所以最小的元素会排在第一位
  • @GuySadoun std::sort(beginning, ending, comparison_function_to_use)

标签: c++ sorting c++11 vector stdvector


【解决方案1】:

我可以想出几种方法来解决这个问题。

解决方案 1

将值 14 分配给 ace。

解决方案 2

在比较函数中考虑到 ace 的特殊性质。

bool Card::operator<(Card &other) {
    int l = (this->worth == 1) ? 14 : this->worth;
    int r = (other.worth == 1) ? 14 : other.worth;
    return (l < r);
}

清理建议。

更改函数以使用 const 对象。

bool Card::operator<(Card const& other) const {
    int l = (this->worth == 1) ? 14 : this->worth;
    int r = (other.worth == 1) ? 14 : other.worth;
    return (l < r);
}

【讨论】:

    【解决方案2】:

    正如@Jon Doe 所写

    我写道:

    sort(sorted_hand.begin(), sorted_hand.end(), cardCompare);
    

    同时:

    bool cardCompare(const Card* lh, const Card* rh) {
        return *lh <= *rh;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-31
      • 2014-05-07
      • 1970-01-01
      相关资源
      最近更新 更多