C++ Array Shuffle

后端 未结 10 1857
温柔的废话
温柔的废话 2021-01-07 14:06

I\'m fairly new to C++ and don\'t quite understand function parameters with pointers and references. I have an array of Cards that I want to shuffle using the Fisher-Yates

10条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-07 14:46

    I think it might help to see the calling code.

    
    class Card{
    public:
        Card(int number):number_(number){}
        int getNumber(){return number_;}
     // ...
    private:
        int number_;
    };
    
    void shuffle (Card * deck[]) {
        int deckSize = 24;
        while (deckSize > 1) {
           long int k = lrand48();
           k = k %24;
           deckSize--;
           Card * temp = deck[deckSize];
           deck[deckSize] = deck[k];
           deck[k] = temp;
        }
    }
    
    int main(int argc, char* argv[]){
    {
    
      const int deckSize=24;
      Card* deck[deckSize];
      for(int i = 0 ; i getNumber()

    That should work just fine.

提交回复
热议问题