cout name of vector in a vector (2D Vector)

My goal is like this: std::cout << Colors[0] << " " << Colors[0][0]; Which would output : Red 0. How would I achieve this?? Or am I thinking about it all wrong

1
2
3
4
5
6
  std::vector<char> Red = { '0','1','1','2','2','3','3','4','4','5','5','6','6','7','7','8','8','9','9', 'R', 'R', 'S', 'S', '@', '@' };
    std::vector<char> Blue = { '0','1','1','2','2','3','3','4','4','5','5','6','6','7','7','8','8','9','9', 'R', 'R', 'S', 'S', '@', '@' };
    std::vector<char> Green = { '0','1','1','2','2','3','3','4','4','5','5','6','6','7','7','8','8','9','9', 'R', 'R', 'S', 'S', '@', '@' };
    std::vector<char> Yellow = { '0','1','1','2','2','3','3','4','4','5','5','6','6','7','7','8','8','9','9', 'R', 'R', 'S', 'S', '@', '@' };
    std::vector<char> Black = { 'W', 'W', 'W', 'W', '$', '$', '$', '$' };
    std::vector<std::vector<char>> Colors = { Red,Blue,Green,Yellow,Black };


I am creating an Uno game and this is the part of creating the deck into a txt file using fstream
Last edited on
I suggest, making a deck of card entities. Here a way how I would do it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
struct Card
{
    char rank;
    char val;
};

class Deck
{
    static std::string val = "0112233445566778899RRSS@@";
    static std::string bval = "WWWW$$$$";
    static std::string rank = "rbgyB";

    std::vector<Card> cards;
    
public:
    Deck()
    {
        // handling the colored cards:
        for( int i = 0; i < rank.size()-1; ++i )
        {
            for( int j = 0; j < val.size(); ++j) {
            }
                cards.push_back( Card( rank[i], val[j] );
            }
        }
        // handling the black cards
        for( int i = 0; i < bval.size(); ++i )
        {
            cards.push_back( Card( rank[rank.size()-1], bval[i]) );
        }
    }
};


If you have this Deck, you need only store your Card-vector into a file stream.
But you don't even do that, for creating a deck you need only store the 'val', 'bval' and 'rank' strings.
Last edited on
The variable you call "Colors" is your deck, right?
I assume that at some point, you'd want to shuffle your deck so that a Red 0 could show up right under a Yellow 4.
If so, then I think it would be better if you had a simple 1-dimensional container of Uno cards.
e.g.
1
2
3
4
struct UnoCard {
    char rank;
    /* int or enum */ color; 
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Example program
#include <iostream>
#include <string>
#include <vector>

enum class UnoColor {
    Red = 0, Blue, Green, Yellow, Black 
};

std::ostream& operator<<(std::ostream& os, const UnoColor& color)
{
    static const char* toStrings[] { "Red", "Blue", "Green", "Yellow", "Black" };
    return os << toStrings[static_cast<int>(color)];
}

struct UnoCard {
    char rank; // or whatever you call it, idk
    UnoColor color;
};

std::ostream& operator<<(std::ostream& os, const UnoCard& card)
{
    return os << card.rank << " " << card.color;
}

// helper function to make adding cards to the deck a bit less tedious
void push_back_range(std::vector<UnoCard>& deck, const std::vector<char>& ranks, UnoColor color)
{
    for (size_t i = 0; i < ranks.size(); i++)
    {
        deck.push_back( { ranks[i], color } );   
    }
}

int main()
{
    std::vector<UnoCard> deck;
    
    // add Red, Blue, Green, Yellow colored cards
    for (int color = 0; color < 4; color++)
    {
        std::vector<char> cards { '0',   
                                  '1', '1', '2', '2', '3', '3',
                                  '4', '4', '5', '5', '6', '6',
                                  '7', '7', '8', '8', '9', '9',
                                  'R', 'R', 'S', 'S', '@', '@' };
                                  
        push_back_range(deck, cards, static_cast<UnoColor>(color));
    }
    
    // add black cards
    {
        std::vector<char> cards { 'W', 'W', 'W', 'W', '$', '$', '$', '$' };
        push_back_range(deck, cards, UnoColor::Black);
    }
    
    // print some cards:
    std::cout << "Size of deck: " << deck.size() << '\n'
              << deck[0] << '\n'
              << deck[11] << '\n'
              << deck[29] << '\n'
              << deck.back() << '\n';
}

Size of deck: 108
0 Red
6 Red
2 Blue
$ Black

I overloaded the operator<< to allow for printing with cout.

___________________________________________________

Also, figure I should note: The actual name of a variable does not exist after the program is compiled. So it doesn't matter that the name of your vector is Red. If you want to print "Red", you must print a string that contains "Red" (see my operator<< for UnoColor, where I use the enum to print an index of an array of c-strings).
Last edited on
Topic archived. No new replies allowed.