• 14
• 12
• 9
• 10
• 13

Using insert on a vector

This topic is 4589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I know this is a really easy thing but I'm just a beginner. Hopefully someone can easily steer me in the right direction. I'm making a Blackjack game and I'm trying to insert a cut card into a deck (which is a vector of cards) at a specific spot. Here's the function. void Shoe::insertCutCard(void) { Card cutCard; int iRandomNum = rand(); int iFifthOfDeck = vCards.size() * (1/5); int iSeventhOfDeck = vCards.size() * (1/7); cutCard.iSuit = 0; cutCard.iValue = 0; cutCard.bFacingUp = false; int iCutPos = (iRandomNum % (iFifthOfDeck - iSeventhOfDeck) + iFifthOfDeck); vCards.insert(iCutPos, cutCard); } I believe that I need to use a iterator when using insert but I cant find a good way to make an interator equal iCutPos. Any help on the matter would be much appreciated.

Share on other sites
Yeah you need to get an iterator for the position.
 vCards.insert(vCards.begin()+iCutPos, cutCard);