# connect vector<int> and vector<char>

Printable View

• 04-27-2006
hdragon
connect vector<int> and vector<char>
So i wanted to create a deck and randomly deal it to user (13 cards). Then sort them in order from 1 to 13. The sorting function must also check to see if the card is heart, diamon.... in order to put them in front or behind.

i'm not sure if this is the fast way to do it, but i still give it a try. I started by create 2 vectors: one is int, one is char. The vector<int> will store four pairs of 1-13, and the vector<char> will store the simple of heart, diamond, club, and spade (13 pairs for each).

Code:

``` vector<int>CardNum; vector<char>CardType; char card[] = {'\03', '\04', '\05', '\06'};  //store the simples that will be push into the vector<char> vector<int>myCard; vector<char>myCardType; void main() {         int count=0;         srand(time(NULL));         for(int i=0; i<4; i++)         {                 for(int j=1; j<=13; j++)                 {                         CardNum.push_back(j);                                }         }         for(i=0; i<13; i++)         {                 for(int j=0; j<4; j++)                 {                         CardType.push_back(card[j]);                 }         }         for(int j=0; j<CardNum.size(); j++)         {                                         cout<<CardNum[j]<<CardType[j]<<" " ;                         if(CardNum[j] == 13)                                 cout<<endl;         }         cout<<endl<<endl;         while(count != 13)         {                 int getNum = rand()%12;                 int getChar = rand()%3;                 myCard.push_back(CardNum[getNum]);                 CardNum.erase(CardNum.begin()+getNum);                 myCardType.push_back(CardType[getChar]);                 CardType.erase(CardType.begin()+getChar);                 count++;         } }```
When an element in the deck's vector being push into the user's vector, that element will be delete out of the deck's vector. Now, if i want to sort the vector<int>, i have to somehow connect the vector<char> with it to. Can anyone help me with this.
• 04-27-2006
Daved
You can't easily connect the two vectors so that sorting or shuffling one affects the other. There are a couple of other ways to do this.

One is to store values from 1-52, and use math to determine the card value and suit. For example, let's say 0 represents hearts, 1 represents diamonds, ... 3 represents spades. Now if you want to find the value for a 7 of spades, just multiply the suit (3) times 13 and add the card value: 3*13 + 7 = 46. So a King of Spades is 3*13 + 13 = 52, and an Ace of Hearts is 0*13 + 1 = 1. To go in the opposite order, use % and /. If you make a couple functions that do the conversions, it will make your coding pretty easy.

Another solution is to encapsulate the card in a class or struct, then use the vector to hold class instances. That way, the suit and value of the card will always be linked.

One other note, your use of rand is probably not the best way to shuffle cards. With your method, you aren't removing the cards you pick from the deck, so every time you select a card, it remains in the deck to potentially be selected again. The best way to get around this is to use random_shuffle from <algorithm> to shuffle the vector and you can just take the first 5 or 13 or however many you want from the shuffled vector.