connect vector<int> and vector<char>

This is a discussion on connect vector<int> and vector<char> within the C++ Programming forums, part of the General Programming Boards category; So i wanted to create a deck and randomly deal it to user (13 cards). Then sort them in order ...

  1. #1
    Computer guy
    Join Date
    Sep 2005
    I'm lost!!!

    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).

    char card[] = {'\03', '\04', '\05', '\06'};  //store the simples that will be push into the vector<char>
    void main()
    	int count=0;
    	for(int i=0; i<4; i++)
    		for(int j=1; j<=13; j++)
    	for(i=0; i<13; i++)
    		for(int j=0; j<4; j++)
    	for(int j=0; j<CardNum.size(); j++)
    			cout<<CardNum[j]<<CardType[j]<<" " ;
    			if(CardNum[j] == 13)
    	while(count != 13)
    		int getNum = rand()%12;
    		int getChar = rand()%3;
    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.
    Hello, testing testing. Everthing is running perfectly...for now

  2. #2
    Registered User
    Join Date
    Jan 2005
    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.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21