Thread: Question about vector/deque

  1. #1
    ---
    Join Date
    May 2004
    Posts
    1,379

    Question about vector/deque

    As you might know I am pretty knew to using the STL I just started looking at it a couple of days ago. Anyway...
    I have two deques. One is my deck of cards (52 elements) the other is my hand which is empty on creation.
    I want to take N cards from the deck and put them into my hand.
    I am so confused as to how to do this I need to see an example of how to copy an element of one deque to another.

    This is what I feel like doing, but it is pretty obvious that it wont work.
    Code:
    void draw_cards(deque<Cards> *hand, deque<Cards> *deck, int num){
      
      for(int i=0;i<num;i++){
        hand->push_front( /*deck[i] element to be inserted here */);
        deck->pop_front();
    
      } 
     
    }
    I am so lost

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Replace /*deck[i] element to be inserted here */ with deck[0]

    This pushes a copy of the first card of the deck into your hand. Then pop_front removes the first card. Then you push the first card of the deck (which used to be the second card) into your hand. Then pop_front removes the first card. Now on top of the deck lies what was originally the third card.

  3. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    I would suggest instead of removing cards from the deck, you just create an index to where you are in the deck. That way you don't have to recreate the deck every hand, just reset the position and re-shuffle your deck.

    that way your method will work, just replace the pop_front with a line to increment you position index.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    "deck[0]" is index 0 of an array of deque<Card> objects.......oops.

    It's better to prefer reference parameters over pointer paramters (plus a few other C++ idioms):
    Code:
    template<typename T>
    void draw_cards(deque<T> &hand, deque<T> &deck, typename deque<T>::size_type num)
    {
        assert(deck.size() >= num);
    
        typename deque<T>::size_type i = 0;
        for (; i < num; ++i)
        {
            hand.push_front(deck[0]);
            deck.pop_front();
        }//for
    }//draw_cards
    gg

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    This works. I didn't use Codeplug's example because right now im trying not to over confuse myself.
    Code:
    void draw_cards(deque<Cards> &hand, deque<Cards> &deck, int num){
      for(int i=0;i<num;i++){
        hand.push_front(deck[0]);
    
        deck.pop_front();
      }  
    }
    What I didn't think about was that deck[0] will be a different element every iteration because of the pop_front()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM