从函数返回向量并更新向量的对象

时间:2014-10-18 14:11:34

标签: c++ vector

我有3个Player个对象,我在函数中作为引用参数传递。 该功能执行以下操作:

  1. 创建3个对象中的vector个,并根据其得分(数据成员)对它们进行排序。
  2. 然后检查第一,第二或第三等级并返回已调整大小的vector
  3. main函数中,当我遍历vector时,我能够打印获胜者及其奖励,但似乎并未更新实际对象。 如何更新对象?

    这是我到目前为止所做的:

    int enterBet(int balance, int maxBet);
    vector <Player> chkWinner(Player &a, Player &b, Player &c, int pot);
    
    bool sorting_method(Player &_1, Player &_2) {
        //Bigger numbers go first
        return _1.m_total > _2.m_total;
    }
    
    int main()
    {
        Player *player1 = new Player;
        Player *player2 = new Player;
        Player *player3 = new Player;
        vector <Player> winnerArr;
        winnerArr.resize(3);
        int pot = 0;
        int betAmount = 0;
        int maxBet = 10;
        player2->m_name = "Kelly";
        player3->m_name = "Martha";
        int x, y, z;
    
        winnerArr[0] = *player1;
        winnerArr[1] = *player2;
        winnerArr[2] = *player3;
    
        sort(winnerArr.begin(), winnerArr.end(), sorting_method);
        cout << winnerArr[0].m_total << ", "
            << winnerArr[1].m_total << ", "
            << winnerArr[2].m_total << endl;
    
        x = winnerArr[0].m_total;
        y = winnerArr[1].m_total;
        z = winnerArr[2].m_total;
    
        if (x == y && x == z){
            cout << "Divide pot by 3" << endl;
            //totalArr.resize(3);
        }
        else if (x == y && x != z){
            cout << "Divide pot by 2" << endl;
            winnerArr.resize(2);
    
        }
        else{
            cout << "Pay full Pot" << endl;
            winnerArr.resize(1);
        }
    
        int payout;
    
        if (winnerArr.size() == 3){
            payout = ((pot * 5) / 3);
        }
        else if (winnerArr.size() == 2){
            payout = ((pot * 5) / 2);
        }
        else if (winnerArr.size() == 1){
            payout = (pot * 5);
    
            for (int i = 0; i < winnerArr.size(); i++){
                cout << winnerArr[i].m_name << " wins " << payout << endl;
                winnerArr[i].m_balance += payout;
                cout << winnerArr[i].m_balance << endl;
    
            }
        }
        cout << player1->m_name << "'s total: " << player1->m_balance << endl;
        cout << player2->m_name << "'s total: " << player2->m_balance << endl;
        cout << player3->m_name << "'s total: " << player3->m_balance << endl;
        break;
    }
    

1 个答案:

答案 0 :(得分:0)

正如大家所指出的,你不应该创建一个新阵列并修改你已经拥有的阵列。 STL帮助您通过排序功能完成您想要的任务。

首先,您需要:

#include <algorithm>

然后你需要一个排序功能:

bool sorting_method(const Player &_1, const Player &_2){
    //Bigger numbers go first
    return _1.m_total > _2.m_total;
}

最后,您使用sort而不是链式循环:

void chkWinner(std::vector <Player> &totalArr){
    sort(totalArr.begin(), totalArr.end(), sorting_method);
    // Add code here, if you need to perform something else. 
    //totalArr is already in the order you need it to.
{

More information about std::sort()

编辑:我修改了代码,创建了一个指向玩家的数组,而不是玩家的副本。

bool sorting_method(Player *_1, Player *_2) {
    //Bigger numbers go first
    return _1->m_total > _2->m_total;
}

int main()
{
    Player *player1 = new Player(2, 1);
    Player *player2 = new Player(5 ,2);
    Player *player3 = new Player(3, 3);
    vector <Player *> winnerArr(3);
    int pot = 0;
    int betAmount = 0;
    int maxBet = 10;
    int x, y, z;

    winnerArr[0] = player1;
    winnerArr[1] = player2;
    winnerArr[2] = player3;

    sort(winnerArr.begin(), winnerArr.end(), sorting_method);
    cout << winnerArr[0]->m_name << ", "
        << winnerArr[1]->m_name << ", "
        << winnerArr[2]->m_name << endl;

请注意,由于此更改,您必须修改尝试访问阵列的所有代码,包括sorting_method。我编写的这段代码已经修改过了,所以你只需要修改其余的代码。我删除了resize函数并在创建数组时添加了(3)参数。我相信它更简单明了。玩得开心:D