获取分段错误(代码在C++中被丢弃)

获取分段错误(代码在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,

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

我是新的编码,所以我不明白

#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;
}

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

你的牌组没有任何元素,你不能以这种方式循环

vector<int> deck;

// Prime, shuffle, dump
for (int i=0; i<52; deck[i++]=i)
{      
  for_each(deck.begin(), deck.end(), print_card);
}
要么在声明后调用resize52,要么直接调用构造函数deck52,在其中放置52个空元素

static const char *ranks[] ={"Ace, Two, ..., King"};
这是一个大小为1的数组,单个元素是整个字符串,这意味着访问秩[]是未定义的行为

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

static const char *ranks[] ={"Ace", "Two", ..., "King"};
西服数组也是如此

您也不建议引用向量中尚不存在的元素,例如当deck为空时,您的deck[i++]=i

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

要将元素附加到向量的后面,可以使用vector::push_back

<>但是,如果你要成为一个C++程序员,你应该完全接受这个语言,包括当用户定义的类型提供更多的保护和表达时,远离基本类型。 您可以使用以下内容生成卡片类:

#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" };

从这里,只需要按需要使用类,在C++中,如果你使用原始整数,C++会更仔细地检查检查,那就舒服了。 您只需添加函数即可执行任何其他处理,例如从牌组中随机获得一张牌,或将其放回

我用于这些类的测试线束是:

int main() {
    Deck deck;
    std::cout << "[" << deck << "]\n";
}
使用创建甲板对象时

vector<int> deck;
由于大小在编译时已知,因此最好使用std::array


由于循环时甲板[0]为空,因此发生了core

可能是这样

    for (int i=0; i<52; i++)
    {
       deck.push_back(i);
    }
    for_each(deck.begin(), deck.end(), print_card);

我确信这些只是粘贴代码时的格式错误。使用std::vector使编程更容易,因为不需要担心本机指针。哎呀!真的xD,我忘记了数组中的元素,现在我了解了vector int的工作原理。谢谢!顺便说一句,我忘了包括以使srandunsigned inttimeNULL;使用调试器向您显示segfault的确切代码行,然后您可以从中推断错误所在。
vector<int> deck;
vector<int> deck(52);
std::array<int, 52> deck;
    for (int i=0; i<52; i++)
    {
       deck.push_back(i);
    }
    for_each(deck.begin(), deck.end(), print_card);
    static const char *ranks[] ={"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    static const char *suits[] ={"Spades", "Clubs", "Diamonds", "Hearts"};