J
J. Campbell
I'm comfortable with arrays from previous programming, and understand
the advantages of c++ vectors...I just don't understand how to use
them :~( Can you help me to use a vector<string> in the following
compilable example instead of the string* array?
Thanks,
Joe
#include <iostream>
#include <string>
//#include <vector>
using namespace std;
class Deck {
private:
string* the_deck; //I want to use vector<string> the_deck
string cards;
string suits;
int decksize;
public:
Deck(string cards_in, string suits_in);
~Deck();
void make_new();
void show();
};
void wait();
const string suits = "\x5\x4\x3\x6";
const string cards = "23456789TJQKA";
//-----------------------------
int main(){
Deck Pack(cards, suits);
Pack.make_new();
Pack.show();
wait();
return 0;
}
//------------------------------
void wait(){
cout << "\n<Enter> to continue...";
string z;
getline(cin,z);
}
Deck:eck(string cards_in, string suits_in){
cards = cards_in;
suits = suits_in;
decksize = suits.size() * cards.size();
the_deck = new string [decksize];
}
Deck::~Deck(){
delete[] the_deck;
}
void Deck::show(){
for(int i = 0; i < (cards.size() * suits.size()); ++i){
if (!(i % cards.size()) && i) cout << '\n';
cout << the_deck << " ";
}
cout << endl;
}
void Deck::make_new(){
cout << "New Deck Created" << endl;
int count = 0;
for(int i = 0; i < suits.size(); ++i)
for(int j = 0; j < cards.size(); ++j){
the_deck[count]=cards[j];
the_deck[count++]+=suits;
}
}
the advantages of c++ vectors...I just don't understand how to use
them :~( Can you help me to use a vector<string> in the following
compilable example instead of the string* array?
Thanks,
Joe
#include <iostream>
#include <string>
//#include <vector>
using namespace std;
class Deck {
private:
string* the_deck; //I want to use vector<string> the_deck
string cards;
string suits;
int decksize;
public:
Deck(string cards_in, string suits_in);
~Deck();
void make_new();
void show();
};
void wait();
const string suits = "\x5\x4\x3\x6";
const string cards = "23456789TJQKA";
//-----------------------------
int main(){
Deck Pack(cards, suits);
Pack.make_new();
Pack.show();
wait();
return 0;
}
//------------------------------
void wait(){
cout << "\n<Enter> to continue...";
string z;
getline(cin,z);
}
Deck:eck(string cards_in, string suits_in){
cards = cards_in;
suits = suits_in;
decksize = suits.size() * cards.size();
the_deck = new string [decksize];
}
Deck::~Deck(){
delete[] the_deck;
}
void Deck::show(){
for(int i = 0; i < (cards.size() * suits.size()); ++i){
if (!(i % cards.size()) && i) cout << '\n';
cout << the_deck << " ";
}
cout << endl;
}
void Deck::make_new(){
cout << "New Deck Created" << endl;
int count = 0;
for(int i = 0; i < suits.size(); ++i)
for(int j = 0; j < cards.size(); ++j){
the_deck[count]=cards[j];
the_deck[count++]+=suits;
}
}