473,782 Members | 2,454 Online
Bytes | Software Development & Data Engineering Community
+ 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(stri ng 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 3395

"J. Campbell" <ma**********@y ahoo.com> wrote in message news:b9******** *************** ***@posting.goo gle.com...
{
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(stri ng 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**********@y ahoo.com> wrote in message
news:b9******** *************** ***@posting.goo gle.com...
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(stri ng 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(cons t 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(stri ng 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 "Initialisi ng 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.u k (remove [no-spam] to reply)
Jul 19 '05 #5
Michael Winter wrote:
"Gianni Mariani" wrote on 22 Sept 03:
....snippitty

Deck::Deck(s tring 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 "Initialisi ng 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.nc su.edu> wrote in message news:<bk******* ***@uni00nw.uni ty.ncsu.edu>...
"J. Campbell" <ma**********@y ahoo.com> wrote in message
news:b9******** *************** ***@posting.goo gle.com...
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.nc su.edu> wrote in message news:<bk******* ***@uni00nw.uni ty.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(cons t 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(va lue) format with mixed results. For example, in the
body of a constructor, bits_per_word is an int:
bits_per_word(s izeof(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.nc su.edu> wrote in message news:<bk******* ***@uni00nw.uni ty.ncsu.edu>...
.... SNIP

Deck::Deck(co nst 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 << "Constructe d 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 << "Constructi on of B:\n";
B b;

std::cout << "\nConstruc tion 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(va lue) 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(s izeof(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>t he_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(stri ng 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
1719
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; // .... // set the values of p1
13
4628
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 is of size 2. How much memory can I save by storing my pointers in c++ arrays, rather than vectors.
8
5115
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 types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
28
1607
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 enters points 3. Using sqrt( (x2-x1)^2 + (y2-y1)^2) ) It calculates the distance
32
4040
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 large amount of data, that I read off using an ifstream object, I don't want to use the push_back method because this grows the vector<vector<double>dynamically, and that will kill my execution time. So, I want to reserve space first, using, of...
8
1879
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 file into a 2d array. I have this to get the file. #include <iostream> #include <string> #include <fstream> using namespace std;
5
1846
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 thought I would go a bit above and beyond the scope of the class and use strings. But I ran into a snag with my getgrades function. The compiler gives me the error: "81 ISO C++ forbids comparison between pointer and integer" here is the code for the...
2
3767
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 when i do the following. if you add a contact with up to 13 fields it will be stored in a data structure. i have a tabbed pane that will show six of the 13 fields for that contact. when you double click the contact i want it to pop up and show all 13...
0
9639
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10146
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9942
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8967
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
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 we have to send another system
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.