473,471 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

help using vector in place of an array

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::Deck(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[i] << " ";
}
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[i];
}
}
Jul 19 '05 #1
9 3369

"J. Campbell" <ma**********@yahoo.com> wrote in message news:b9**************************@posting.google.c om...
{
private:
string* the_deck; //I want to use vector<string> the_deck
vector<string> the_deck;
the_deck = new string [decksize];
the_deck.resize(decksize);
delete[] the_deck;


delete this line.

The program works fine (apparently).
Jul 19 '05 #2
J. Campbell wrote:
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
Here's a swag:

#include <iostream>
#include <string>
//#include <vector>
using namespace std;

class Deck {
private:
// string* the_deck; //I want to use vector<string> the_deck
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::Deck(string cards_in, string suits_in) : cards( cards_in ),
suits( suits_in ),
decksize( suits.size() * cards.size() ),
the_deck( suits.size() * cards.size() )
{

use the member initialization syntax.
// cards = cards_in;
// suits = suits_in;
// decksize = suits.size() * cards.size();
// the_deck = new string [decksize];
}

Deck::~Deck(){
no delete - a vector will destroy elements in the vector
// 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[i] << " ";
}
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[i];
}
}


Jul 19 '05 #3
"J. Campbell" <ma**********@yahoo.com> wrote in message
news:b9**************************@posting.google.c om...
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::Deck(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[i] << " ";
}
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[i];
}
}


Sure this, here it is:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Deck {
private:
string cards;
string suits;
vector<string> the_deck;

public:
Deck(const string& cards_in, const string& suits_in);

void make_new();
void show() const;
};

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::Deck(const string& cards_in, const string& suits_in)
: cards(cards_in), suits(suits_in), the_deck(cards.size() * suits.size()) {}

void Deck::show() const {
for(unsigned int i = 0; i < the_deck.size(); ++i) {
if(!(i % cards.size()) && i) cout << endl;
cout << the_deck[i] << " ";
}
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[i];
}
}
}

Notice empty constructor body due to initialization list. Also gor rid of
one int variable in your class, as well as the constructor. Compiles and
works just fine under mingw GCC 3.2.3

Hope this helps.

Martin
Jul 19 '05 #4

"Gianni Mariani" wrote on 22 Sept 03:
J. Campbell wrote:
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


Here's a swag:

#include <iostream>
#include <string>
//#include <vector>
using namespace std;

class Deck {
private:
// string* the_deck; //I want to use vector<string> the_deck


vector<string> the_deck;
string cards;
string suits;
int decksize;
public:
Deck(string cards_in, string suits_in);
[SNIP]
Deck::Deck(string cards_in, string suits_in)

: cards( cards_in ),
suits( suits_in ),
decksize( suits.size() * cards.size() ),
the_deck( suits.size() * cards.size() )
{

use the member initialization syntax.

According to the C++ Language Reference in the MSDN Library (sorry, I
don't have the actual C++ specification), that initialisation may not
work correctly.

From "Initialising Member Objects":

"The order in which the member initializers are specified in the
constructor does not affect the order in which the members are
constructed; the members are constructed in the order in which they
are declared in the class."

[From MSDN C++ Language Reference > Special Member Functions >
Initialization Using Special Member Functions > Initializing Bases and
Members > Initializing Member Objects]

And, the order in which they were declared in the class was 'the_deck'
first, then the remaining members (in the order you list them). If
the MSDN Library is correct here, 'the_deck' won't initialize
correctly as 'cards' and 'suits' haven't been initialised. So, is it
(MSDN) correct?

Mike

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
Jul 19 '05 #5
Michael Winter wrote:
"Gianni Mariani" wrote on 22 Sept 03:
....snippitty

Deck::Deck(string cards_in, string suits_in)
: cards( cards_in ),
suits( suits_in ),
decksize( suits.size() * cards.size() ),
the_deck( suits.size() * cards.size() )
{

use the member initialization syntax.



Yikes !


According to the C++ Language Reference in the MSDN Library (sorry, I
don't have the actual C++ specification), that initialisation may not
work correctly.

From "Initialising Member Objects":

"The order in which the member initializers are specified in the
constructor does not affect the order in which the members are
constructed; the members are constructed in the order in which they
are declared in the class."
Yep - you're right.

Very sloppy. Bad bad bad. Grot, even.

I had meant to use the parameters like this ...

decksize( suits_in.size() * cards_in.size() )
the_deck( suits_in.size() * cards_in.size() )

[From MSDN C++ Language Reference > Special Member Functions >
Initialization Using Special Member Functions > Initializing Bases and
Members > Initializing Member Objects]

And, the order in which they were declared in the class was 'the_deck'
first, then the remaining members (in the order you list them). If
the MSDN Library is correct here, 'the_deck' won't initialize
correctly as 'cards' and 'suits' haven't been initialised. So, is it
(MSDN) correct?
MSDN is probably correct.

In general, I try not to rely on the order of construction because I
think it's asking for trouble when someone "rearranges" some of the
class members. It's not to say that you can't use the order of
initialization to your benefit, it's just that you probably need to note
it in the comments of the members very clearly that order is important
and only do that when there is no other alternative.

Mike


Good catch.

Jul 19 '05 #6
"Marcin Vorbrodt" <mv*****@eos.ncsu.edu> wrote in message news:<bk**********@uni00nw.unity.ncsu.edu>...
"J. Campbell" <ma**********@yahoo.com> wrote in message
news:b9**************************@posting.google.c om...
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

Sure this, here it is:

Notice empty constructor body due to initialization list. Also gor rid of
one int variable in your class, as well as the constructor. Compiles and
works just fine under mingw GCC 3.2.3

Hope this helps.

Martin


It helps a ton. Thanks-JC
Jul 19 '05 #7
"Marcin Vorbrodt" <mv*****@eos.ncsu.edu> wrote in message news:<bk**********@uni00nw.unity.ncsu.edu>...

Sure this, here it is:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Deck {
private:
string cards;
string suits;
vector<string> the_deck;

public:
Deck(const string& cards_in, const string& suits_in);

void make_new();
void show() const;
};

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::Deck(const string& cards_in, const string& suits_in)
: cards(cards_in), suits(suits_in), the_deck(cards.size() * suits.size()) {}

Questions about this construct--
What is the significance of the colon?
What is the name of the colon operator when used in this manner?
What is the advantage of, eg, cards(cards_in); assignment before the
body of the constructor compared to cards = cards_in; inside the
constructor?
If you are using the form you showed, why seed the constants in the
constructor definition, rather than in the class declaration?
And finally, I tried initializing some variables with the
variablename(value) format with mixed results. For example, in the
body of a constructor, bits_per_word is an int:
bits_per_word(sizeof(unsigned int) * CHAR_BIT); //doesn't compile
bits_per_word = sizeof(unsigned int) * CHAR_BIT; //compiles fine

Thanks again--JC

void Deck::show() const {
for(unsigned int i = 0; i < the_deck.size(); ++i) {
if(!(i % cards.size()) && i) cout << endl;
cout << the_deck[i] << " ";
}
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[i];
}
}
}

Notice empty constructor body due to initialization list. Also gor rid of
one int variable in your class, as well as the constructor. Compiles and
works just fine under mingw GCC 3.2.3

Hope this helps.

Martin

Jul 19 '05 #8
J. Campbell wrote:
"Marcin Vorbrodt" <mv*****@eos.ncsu.edu> wrote in message news:<bk**********@uni00nw.unity.ncsu.edu>...
.... SNIP

Deck::Deck(const string& cards_in, const string& suits_in)
: cards(cards_in), suits(suits_in), the_deck(cards.size() * suits.size()) {}

Questions about this construct--
What is the significance of the colon?


Syntax. This is valid only on a constructor definition to define an
initializer list.
What is the name of the colon operator when used in this manner?
It's not an operator. It just signifies there is an initializer list.
Call it syntactic sugar. I'm not familiar will all the ambiguities it
might resolve but I'd say that it makes error detection of missing
semicolons a little easier.
What is the advantage of, eg, cards(cards_in); assignment before the
body of the constructor compared to cards = cards_in; inside the
constructor?
Check out this code.

#include <iostream>

struct Element
{
int a_value;

Element( int value = 0 )
: a_value( value )
{
std::cout << "Constructed with value " << a_value << "\n";
}

Element & operator = ( int value )
{
a_value = value;
std::cout << "Assignment with value " << a_value << "\n";
return *this;
}
};

struct B
{
Element v;

B()
: v( 1111 )
{
}
};

struct C
{
Element v;

C()
{
v = 4444;
}
};
int main()
{
std::cout << "Construction of B:\n";
B b;

std::cout << "\nConstruction of C:\n";
C c;
}
Construction of B:
Constructed with value 1111

Construction of C:
Constructed with value 0
Assignment with value 4444

You'll notice that if you use the initializer list ( like we have in B)
there is only construction involved.
If you are using the form you showed, why seed the constants in the
constructor definition, rather than in the class declaration?
What constants ?
And finally, I tried initializing some variables with the
variablename(value) format with mixed results.
The TYPE( <parameters> ) syntax is only valid for calling the constructor.

int c( 2 ); // OK

c( 3 ); // NOT ok

c = int( 3 ); // OK contructs temporary int and assigns

Incidently

int c = 2; // is exactly the same as:
int c(2);

For example, in the body of a constructor, bits_per_word is an int:
bits_per_word(sizeof(unsigned int) * CHAR_BIT); //doesn't compile
expected - you need assignment here since no object is being constructed
(except for temporary int).
bits_per_word = sizeof(unsigned int) * CHAR_BIT; //compiles fine


plain vanilla expression - should work fine.

Jul 19 '05 #9
<Joe>
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?
</>

Cool :-)

#include <iostream.h>
#include <string>
#include <vector>
//using namespace std;

class Deck {
private:
// string* the_deck; //I want to use vector<string> the_deck
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::Deck(string cards_in, string suits_in)
:
the_deck(52)
{
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[i] << " ";
}
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[i];
}
}


Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Jinjun Xu | last post by:
Hi, I have an array and I want to adjust the size (remove some elements). I use the following code. Can you help me to check whether it's correct. ////////////// start int *p1 = new int; ...
13
by: Ben | last post by:
I have a program which is using a lot of memory. At the moment I store a lot of pointers to objects in std::vector. (millions of them) I have three questions: 1) Lets say the average Vector...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
28
by: Nutkin | last post by:
Basicly i have to make a program that calculates the distance between x and y points in 2d space. the code basicly goes like this 1. User says how many points they have (max of 10) 2. User...
32
by: T. Crane | last post by:
Hi, I'm struggling with how to initialize a vector<vector<double>> object. I'm pulling data out of a file and storing it in the vector<vector<double>object. Because any given file will have a...
8
by: isaac2004 | last post by:
hello, i posted with a topic like this but got no real feedback(prob cuz of lapse in my explanation) so i am trying it again. i am trying to set up a function that brings in a txt file and adds the...
5
by: Kelth.Raptor | last post by:
Im having some difficulty with strings here, I hope someone is kind enough to help, I do appreciate it. Im working on a grade point average calculator for my intro to programming class and I...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.