CPP 代码出现第 7 天出现分段错误

问题描述 投票:0回答:1

每当我更改默认为分段错误的代码时,我都会遇到此程序的问题。如果我没有以正确的方式对玩家进行排序,该程序就可以正常工作。感觉像是一种未定义的行为。

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <ostream>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace parttwo {
    struct Game
    {
        struct Player
        {
            struct Hand
            {
                struct Card
                {   
                    enum CardType : int
                    {
                        JACK = 1
                        , TWO = 2
                        , THREE
                        , FOUR
                        , FIVE
                        , SIX
                        , SEVEN
                        , EIGHT
                        , NINE
                        , TEN
                        , QUEEN
                        , KING
                        , ACE
                    } type;
                    char value;
                    operator CardType& ()
                    {
                        return type;
                    }
                    friend bool operator >(const Card& a, const Card& b)
                    {
                        return a.type > b.type;
                    }
                    friend bool operator <(const Card& a, const Card& b)
                    {
                        return a.type < b.type;
                    }
                    friend bool operator ==(const Card& a, const Card& b)
                    {
                        return a.type == b.type;
                    }
                    friend std::istream& operator >>(std::istream& in, Card& card)
                    {
                        in >> card.value;
                        switch (card.value) 
                        {
                            case 'T':
                            {
                                card.type = TEN;
                                break;
                            }
                            case 'J':
                            {
                                card.type = JACK;
                                break;
                            }
                            case 'Q':
                            {
                                card.type = QUEEN;
                                break;
                            }
                            case 'K':
                            {
                                card.type = KING;
                                break;
                            }
                            case 'A':
                            {
                                card.type = ACE;
                                break;
                            }
                            default:
                            {
                                if(card.value >= '2' and card.value <='9')
                                {
                                    card.type = CardType(card.value - '0');
                                }
                                else 
                                {
                                    std::cerr << "Can't find such a card type " << card.value << std::endl;
                                    exit(EXIT_FAILURE);
                                }
                                break;
                            }
                        }
                        return in;
                    }
                };
                std::vector<Card> cards;
                friend std::istream& operator >>(std::istream& in, Hand& hand)
                {
                    Card card;
                    while(in >> card)
                    {
                        hand.cards.push_back(card);
                    }
                    return in;
                }
                friend std::ostream& operator <<(std::ostream& out, const Hand& hand)
                {
                    for(int i = 0; i < hand.cards.size(); i++)
                    {
                        out << hand.cards[i].value; 
                    }
                    return out;
                }
                friend bool operator >(const Hand& a, const Hand& b)
                {
                    std::unordered_map<int, int> um_a, um_b;
                    for(auto &elt : a.cards)
                    {
                        um_a[elt.type]++;
                    }
                    for(auto &elt : b.cards)
                    {
                        um_b[elt.type]++;
                    }
                    int aj_count = 0, bj_count = 0;
                    std::vector<std::vector<int>> vv_a, vv_b;
                    std::transform(um_a.begin(), um_a.end(), std::back_inserter(vv_a), [](auto& e){ return std::vector<int>{e.second, e.first}; });
                    std::transform(um_b.begin(), um_b.end(), std::back_inserter(vv_b), [](auto& e){ return std::vector<int>{e.second, e.first}; });                    
                    if(vv_a.size() > 1)
                    for(int i = 0; i < vv_a.size() ; i++)
                    {
                        if(vv_a[i][1] == Card::JACK)
                        {
                            aj_count = vv_a[i][0];
                            vv_a.erase(vv_a.begin()+i);
                            i--;
                            break;
                        }
                    }
                    if(vv_b.size() > 1)
                    for(int i = 0; i < vv_b.size(); i++)
                    {
                        if(vv_b[i][1] == Card::JACK)
                        {
                            bj_count = vv_b[i][0];
                            vv_b.erase(vv_b.begin()+i);
                            i--;
                            break;
                        }
                    }       
                    std::make_heap(vv_a.begin(), vv_a.end());
                    std::make_heap(vv_b.begin(), vv_b.end());
                    vv_a[0][0] += aj_count;
                    vv_b[0][0] += bj_count;
                    // if(vv_a.size() < vv_b.size()) return true;
                    // else if(vv_a.size() > vv_b.size()) return false;
                    int mn = std::min(vv_a.size(), vv_b.size());
                    bool greater = false;
                    for(int i = 0; i < mn; i++)
                    {
                        if(vv_a[i][0] > vv_b[i][0]) {greater = true; break;}
                    }
                    return greater or a.cards > b.cards;
                }
            };
            Hand hand;
            using bid_t = int;
            bid_t bid;
            friend std::istream& operator >>(std::istream& in, Player& player)
            {
                std::string segment;
                std::getline(in, segment, ' ');
                std::istringstream ss1(segment);
                ss1 >> player.hand;
                std::getline(in, segment, ' ');
                std::istringstream ss2(segment);
                ss2 >> player.bid;
                return in;
            }
        };
        std::vector<Player> players;
        friend std::istream& operator >>(std::istream& in, Game& game)
        {
            std::string line;
            while(std::getline(in, line, '\n'))
            {
                if(line != "")
                {
                    std::istringstream ss(line);
                    Player player;
                    ss >> player;
                    game.players.push_back(player);
                }
            }
            return in;
        }

    };
    std::string test = R"(
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483
)";
    std::string test2 = R"(
T997Q 289
92QA9 726
AJ394 900
AQ394 9220
JJJJJ 565
AAAKQ 678
AAAKK 666
AAA22 77
AAA25 77
AAA33 77
QQQQ5 88
QQQQ5 88
AAKKQ 99
AAKKK 89
23456 90
23458 888
)";
    int program()
    {
        std::istringstream ss(test2);
        std::ifstream file("input.txt");
        std::istream& in = ss;
        Game game;
        in >> game;
        std::sort(std::begin(game.players), std::end(game.players), [](const Game::Player& a, const Game::Player& b)
        {
                return b.hand > a.hand; 
        });
        // std::cout << game.players.size() << std::endl;
        // bool x = game.players[0].hand > game.players[1].hand;
        int total = 0;
        for(int i = 0; i < game.players.size(); i++)
        {
            std::cout << game.players[i].hand << std::endl;
            total += (i+1) * game.players[i].bid;
        }

        std::cout << total << std::endl;
        return 0;
    }
}

int main()
{
    using namespace parttwo;
    program();
    return 0;
}

我尝试根据手牌对玩家进行排序,希望玩家能够正确排序。这是第七天的第二部分。问题是这样的。您应该找到一手牌从最低到最高价值的所有总和(出价 * 玩家排名)。五张牌是指所有 5 张牌为一种类型。四张同种牌是 4 / 5 张相同的牌。如果您了解扑克,您可能会猜到其余的内容,但它们是从最低级别到最高级别的。 Jack 计入出现次数最多或价值最高的卡片。在比较函数中,我尝试查找出现次数最多的卡片,然后将杰克计数添加到出现次数最多或值最高的卡片上,然后查找出现次数最多的卡片。

c++11 c++14
1个回答
0
投票

有人向我展示了 godbolt.org 上运行的代码。它可以在那里工作,但不能在我的电脑上

© www.soinside.com 2019 - 2024. All rights reserved.