c++ - 获取段错误(代码转储在 C++ 中

标签 c++

这里遇到了什么问题?我很困惑? 该程序应显示一副洗好的牌。

我是编码新手,所以我不明白...

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

// names of ranks.
static const char *ranks[] ={"Ace, Two, Three, Four, Five, Six, Seven, 
                              Eight, Nine, Ten, Jack, Queen, King"};

// name of suites
static const char *suits[] ={"Spades, Clubs, Diamonds, Hearts"};

void print_card(int n)
{
    cout << ranks[n % 13] << " of " << suits[n / 13] << endl;
}

int main()
{
    srand((unsigned int)time(NULL));

    vector<int> deck;

    // Prime, shuffle, dump
    for (int i=0; i<52; deck[i++]=i)
    {
        for_each(deck.begin(), deck.end(), print_card);
    }
    return 0;
}

我收到一个错误(分段),我不知道它是什么 :(

最佳答案

static const char *ranks[] ={"Ace, Two, ..., King"};

这是一个大小为one, 的数组,单个元素是整个字符串,意味着访问ranks[<anything other than zero>]。是未定义的行为。

你需要的是一个不同字符串的数组,例如:

static const char *ranks[] ={"Ace", "Two", ..., "King"};

同上 suits数组。


在 vector 中引用尚不存在的元素也是不明智的,例如您的 deck[i++]=i什么时候deck是空的。

要设置现有 元素,您可以使用该方法(尽管它不像vector::at() 那样进行边界检查,因此初学者可能应该使用它)。

追加元素到 vector 的后面,你可以使用vector::push_back() .


但是,如果您要成为一名 C++ 编码员,您应该完全接受这门语言,包括在用户定义类型提供更多保护和表现力时远离基本类型。

您可以生成 Card用类似的东西上课:

#include <iostream>
#include <sstream>
#include <string>
#include <vector>

class Card {
public:
    enum Rank { Ace, Two, Three, Four, Five, Six, Seven, Eight,
        Nine, Ten, Jack, Queen, King, EndRank };
    enum Suit { Spades, Clubs, Diamonds, Hearts, EndSuit };

    explicit Card(Rank rank = Ace, Suit suit = Spades)
        : m_rank(rank), m_suit(suit) {}

    std::string ToString() const {
        return m_rankLookup[m_rank] + " of " + m_suitLookup[m_suit];
    }

private:
    const Rank m_rank;
    const Suit m_suit;

    static const std::vector<std::string> m_rankLookup;
    static const std::vector<std::string> m_suitLookup;

    friend std::ostream &operator<<(std::ostream &os, const Card& me) {
        return os << me.ToString();
    }
};

const std::vector<std::string> Card::m_rankLookup { "Ace", "Two", "Three",
    "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack",
    "Queen", "King" };
const std::vector<std::string> Card::m_suitLookup { "Spades", "Clubs",
    "Diamonds", "Hearts" };

然后您可以在此基础上使用 Deck类,沿着:

class Deck {
public:
    explicit Deck(unsigned numDecks = 1) {
        for (unsigned i = 0; i < numDecks; ++i) {
            for (auto suit = Card::Spades;
                suit != Card::EndSuit;
                ++(*reinterpret_cast<int*>(&suit)))
            {
                for (auto rank = Card::Ace;
                    rank != Card::EndRank;
                    ++(*reinterpret_cast<int*>(&rank)))
                {
                    m_deck.push_back(Card(rank, suit));
                }
            }
        }
    }

    std::string ToString() const {
        if (m_deck.size() == 0) {
            return "";
        }
        std::stringstream ss;
        ss << m_deck[0];
        for (unsigned i = 1; i < m_deck.size(); ++i) {
            ss << ", " << m_deck[i];
        }
        return ss.str();
    }

private:
    std::vector<Card> m_deck;

    friend std::ostream &operator<<(std::ostream &os, const Deck& me) {
        return os << me.ToString();
    }
};

从那里开始,只需根据需要使用类即可,因为知道 C++ 将对事物进行类型检查比使用原始整数要彻底得多。

您只需添加函数来执行任何其他处理,例如从牌组中随机获取一张牌,或将其放回原位。

我在这些类(class)中使用的测试工具是:

int main() {
    Deck deck;
    std::cout << "[" << deck << "]\n";
}

我得到的输出是(为了便于阅读而重新格式化):

[Ace of Spades, Two of Spades, Three of Spades, Four of Spades,
 Five of Spades, Six of Spades, Seven of Spades, Eight of Spades,
 Nine of Spades, Ten of Spades, Jack of Spades, Queen of Spades,
 King of Spades, Ace of Clubs, Two of Clubs, Three of Clubs,
 Four of Clubs, Five of Clubs, Six of Clubs, Seven of Clubs,
 Eight of Clubs, Nine of Clubs, Ten of Clubs, Jack of Clubs,
 Queen of Clubs, King of Clubs, Ace of Diamonds, Two of Diamonds,
 Three of Diamonds, Four of Diamonds, Five of Diamonds,
 Six of Diamonds, Seven of Diamonds, Eight of Diamonds,
 Nine of Diamonds, Ten of Diamonds, Jack of Diamonds,
 Queen of Diamonds, King of Diamonds, Ace of Hearts, Two of Hearts,
 Three of Hearts, Four of Hearts, Five of Hearts, Six of Hearts,
 Seven of Hearts, Eight of Hearts, Nine of Hearts, Ten of Hearts,
 Jack of Hearts, Queen of Hearts, King of Hearts]

关于c++ - 获取段错误(代码转储在 C++ 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57175806/

相关文章:

c++ - winapi listview检查项c++

c++ - 在 C++ 中为空 ptr-to-ptr 赋值

c++ - 如何仅从源代码构建 TensorFlow lite 而不是所有 TensorFlow?

c++ - 复制构造函数和 move 构造函数如何工作

c++ - 在 C/C++ : is it possible? 中分配 CPU 缓存中的静态内存

c++ - Bit-Shift 一个字符串来解码保存文件 C++

c++ - C/C++ 构建通用栈帧来调用不同的回调函数

c++ - 为什么 std::shared_ptr<T[]> 没有专门化?

c++ - 如何判断路径是否来自 Active Directory 重定向文件夹?

c++ - 如何确定 C++ 对象是否已被释放?