Assigning from one vector to another vector - HELP!

Alright, so I have my blackjack code slowly developing. Now, I have a vector of cards which is my deck. It is called:
[code]vector v;[/code] I also now have a vector for the dealer's hand and a vector for the player's hand:
[code]vector dealer;
vector user;[/code]
When I deal a card, I have to take the top card in the deck and put it into the 0th spot in the player's hand, then the next card into the 0th spot in the dealer's hand. I've tried various things with no success. Can someone help me figure out basically how to say:
[code]v[0] = dealer[0];
v[0] = user[0];[/code]
Where the 0th element of the deck vector is removed from the deck and placed into the respected hands? Thanks!


Comments

  • To remove the first element of the vector use the erase member function.
    It takes a iterator. The iterator for the first element is conveniently returned by the begin member function.

    [code]
    v.erase(v.begin());
    [/code]

    So what you'd do is:
    [code]
    dealer[0] = v[0];
    v.erase(v.begin());
    player[0] = v[0];
    [/code]

    Hope that helps.
  • Thanks! Ok, so now I got it to compile, and it looks like this:
    [code]void Deck::Deal (void)
    {
    dealer[0] = v[0];
    v.erase(v.begin());

    user[0] = v[0];
    v.erase(v.begin());

    cout << dealer[0] << endl;
    cout << user[0] << endl;

    }[/code]

    In main, when I call the Deal function, i use[code]d->Deal();[/code] but when it runs, it gets as far in the code as the call to deal, and then crashes. Any idea why?

    : To remove the first element of the vector use the erase member function.
    : It takes a iterator. The iterator for the first element is conveniently returned by the begin member function.
    :
    : [code]
    : v.erase(v.begin());
    : [/code]
    :
    : So what you'd do is:
    : [code]
    : dealer[0] = v[0];
    : v.erase(v.begin());
    : player[0] = v[0];
    : [/code]
    :
    : Hope that helps.
    :

  • [b][red]This message was edited by the PeterTheMaster at 2002-6-30 11:51:5[/red][/b][hr]
    dealer and user dont have any memory allocated yet. so you cant put anything on their 0.th position because this position doesnt exist yet.
    there are functions to reserve memory, if youre interested, look up resize or reserve or something. dont remember.
    but the better way here would be push_back
    (didnt i suggest push_back already as solution for your last problem? :-) )

    try this. the [] works for vector, but i dont like it because
    if you lateron decide to use another sequence container, like a list,
    you dont have to change anything if you use the more general iterator
    approach to access elements.

    [code]void Deck::Deal (void)
    {
    dealer.push_back(v.front());
    v.erase(v.begin());

    user.push_back(v.front());
    v.erase(v.begin());

    cout << dealer.front() << endl;
    cout << user.front() << endl;

    }[/code]




  • Peter,
    Thanks! I thought of that while I was away from my computer last night. Once I got back home and plugged that in, it's working GREAT! Thanks again!

    : [b][red]This message was edited by the PeterTheMaster at 2002-6-30 11:51:5[/red][/b][hr]
    : dealer and user dont have any memory allocated yet. so you cant put anything on their 0.th position because this position doesnt exist yet.
    : there are functions to reserve memory, if youre interested, look up resize or reserve or something. dont remember.
    : but the better way here would be push_back
    : (didnt i suggest push_back already as solution for your last problem? :-) )
    :
    : try this. the [] works for vector, but i dont like it because
    : if you lateron decide to use another sequence container, like a list,
    : you dont have to change anything if you use the more general iterator
    : approach to access elements.
    :
    : [code]void Deck::Deal (void)
    : {
    : dealer.push_back(v.front());
    : v.erase(v.begin());
    :
    : user.push_back(v.front());
    : v.erase(v.begin());
    :
    : cout << dealer.front() << endl;
    : cout << user.front() << endl;
    :
    : }[/code]
    :
    :
    :
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories