Is there an easier way to instantiate many objects?

My teacher once told me if I am using a lot of repetitive code chances are I'm doing something wrong or inefficiently. What I'm trying to do is create a deck of card but instantiating each card (52) seems very redundant. Am i doing this correctly or is there a better method to do this? Keep in mind I am still very much a beginner. This is what I've done so far:

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
  #include <iostream>
#include <vector>

using namespace std;

class CardDeck
{
public:
    string suite;
    int suiteStrength;
    int num;
};

int main()
{
    //Holds all Diamond cards
    vector<CardDeck> diamondSuite;
    
    //Create all
    CardDeck threeDiamond;
    diamondSuite.push_back(threeDiamond);
    CardDeck fourDiamond;
    diamondSuite.push_back(fourDiamond);
    CardDeck fiveDiamond;
    diamondSuite.push_back(fiveDiamond);
    CardDeck sixDiamond;
    diamondSuite.push_back(sixDiamond);
    CardDeck sevenDiamond;
    diamondSuite.push_back(sevenDiamond);
    CardDeck eightDiamond;
    diamondSuite.push_back(eightDiamond);
    CardDeck nineDiamond;
    diamondSuite.push_back(nineDiamond);
    CardDeck tenDiamond;
    diamondSuite.push_back(tenDiamond);
    CardDeck jackDiamond;
    diamondSuite.push_back(jackDiamond);
    CardDeck queenDiamond;
    diamondSuite.push_back(queenDiamond);
    CardDeck kingDiamond;
    diamondSuite.push_back(kingDiamond);
    CardDeck aceDiamond;
    diamondSuite.push_back(aceDiamond);
    CardDeck twoDiamond;
    diamondSuite.push_back(twoDiamond);
    
    for(int i = 3; i < 13; i ++)
    {
        diamondSuite[i].suite = "DIAMOND";
        diamondSuite[i].suiteStrength = 0;
        diamondSuite[i].num = i;
    }
    
    cout << "SUITE:" << diamondSuite[3].suite << endl;
    cout << "SUITE STR: " << diamondSuite[7].suiteStrength << endl;
    cout << "NUM: " << diamondSuite [12].num << endl;
}


I was thinking of switching to pairs since all i would need is suiteStrength and num but it would be harder to read.
Last edited on

Am i doing this correctly or is there a better method to do this?

Of course there's a more efficient method to initialise a vector with 13 objects. You can use the fill constructor to do just that.
1
2
3
    const int cards_per_suit = 13;
    //Holds all Diamond cards
    vector<CardDeck> diamondSuite( cards_per_suit );

http://www.cplusplus.com/reference/vector/vector/vector/

Also, it's "suit" and not "suite".
Last edited on
Your snippet between lines 17 and 52 is equivalent to
1
2
3
4
CardDeck card_template = {"DIAMOND", 0, 0};
vector<CardDeck> diamondSuite(13, card_template);
for(int i = 3; i < 13; i++)
    diamondSuite[i].num = i;

By the way, having a class called CardDeck that represents a single card is odd.
@integrafix

1
2
3
const int cards_per_suit = 13;
    //Holds all Diamond cards
    vector<CardDeck> diamondSuite( cards_per_suit );


Does this mean you initialized a vector that contains 13 elements of the CardDeck type? That is wayyyyyyyyyyy more efficient. And i also realized it was suit after i posted haha. And also is it better to put const int cards_per_suit = 13; in main or global?

@helios

Can you explain the template part?

 
CardDeck card_template = {"DIAMOND", 0, 0};


I think i get the gist of the code but im still a novice programmer and the first line is a little confusing. Is that a template class? Or am i confusing this with something else? And yea it does sound weird but it was because i was initially going to create a class for the entire deck when i just needed it for a single card template.
Last edited on
No, it's just an object called card_template whose members suite, suiteStrength, and num are each initialized to "DIAMOND", 0, and 0, respectively.
Topic archived. No new replies allowed.