A debt of gratitude

Thank you very much to CTphpnwb on </ dream.in.code> for helping me to gain a better understanding of how to work with references – and why this is important.

The basic problem I was facing was that I was passing the whole factor of objects into my functions. When you do this, it actually creates a copy of the vector and manipulates that. What I wanted to have happen was to pass a reference to my vector (i.e. the vector’s address in the computer) so that no copy was made, and all changes were done to the original vector.

I’ve tried playing with shrink_to_fit, but I think I may be exploring territory that isn’t going to actually help me at this point.

the successful code:

//

//  main.cpp

//  clowncar

//

//  Created by Jack on 5/4/14.

//  Copyright (c) 2014 Jack Treml. All rights reserved.

//

 

 

 

/*

 1. empty car (0)

 2. pick up Pennywise and John Wayne Gacy (2)

 3. Pick up Bozo and Krusty (4)

 4. Pick up Ronald McDonald (5)

 5. print out list of clowns

 6. As for a name of a clown (or ID number) to kick out of the car

 7. Kick only that individual out, then re-print list

 */

 

#include <iostream>

#include <vector>

usingnamespacestd;

using std::vector;

 

class IdNumbers{     // a place to store a counter for creating idNumbers

public:

    IdNumbers(){

    }

static int getANewNumber(){

counter++;

return counter;

    }

protected:

static int counter;

};

 

class Clown{   // the clown objects

public:

    Clown(){};

    ~Clown(){};

    Clown(int clownId){

idNum = clownId;

    };

    Clown(string nameOfClown, IdNumbers theNumber){

clownName = nameOfClown;

idNum = theNumber.getANewNumber();

    };

int getClownId(void){

return idNum;

    }

void printClown(void){

cout<<“idNumber = “<<idNum<<endl;

cout<<“name = “<<clownName<<endl;

    };

void printClown2(void){

        cout<<“Clown idNumber = “<<idNum<<“, name = “<<clownName<<endl;

    };

private:

string clownName;

int idNum;

};

 

//functions

void printVectorContents(vector<Clown> &);

void printVectorContents2(vector<Clown> &);

void askWhoToPop(vector<Clown> &);

void popClown(vector<Clown> &, int);

void popThisClown(vector<Clown> &car, int popWho);

 

int IdNumbers ::counter = 0;

 

int main(int argc, const char * argv[])

{

IdNumbers theCounter;

    vector<Clown> clowncar;     //sets up a vector for fish

int vectorSize = 0;

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

cout<<“vector is size: “<<vectorSize<<endl;

Clown pennywise(“Pennywise”, theCounter);

    clowncar.push_back(pennywise);

Clown jwgacy(“John Wayne Gacy”, theCounter);

    clowncar.push_back(jwgacy);

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

cout<<“vector is size: “<<vectorSize<<endl;

    //printVectorContents(clowncar);

    printVectorContents2(clowncar);

Clown bozo(“Bozo”, theCounter);

    clowncar.push_back(bozo);

Clown krusty(“Krusty”, theCounter);

    clowncar.push_back(krusty);

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

cout<<“vector is size: “<<vectorSize<<endl;

    //printVectorContents(clowncar);

    printVectorContents2(clowncar);

Clown ronald(“Ronald McDonald”, theCounter);

    clowncar.push_back(ronald);

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

    cout<<“current capacity is: “<<clowncar.capacity()<<endl;

cout<<“vector is size: “<<vectorSize<<endl;

    //printVectorContents(clowncar);

    //list clowns with ID numbers

    printVectorContents2(clowncar);

    //ask for an ID number of a clown to evict

askWhoToPop(clowncar);

    cout<<“**********”<<endl;

    clowncar.shrink_to_fit();

    cout<<“current capacity is: “<<clowncar.capacity()<<endl;

cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents2(clowncar);

return 0;

}

 

 

//Non-Member functions

void printVectorContents(vector<Clown> &car){

for(int i=0; i < car.size(); i++){

        car[i].printClown();

    }

}

 

void printVectorContents2(vector<Clown> &car){

for(int i=0; i < car.size(); i++){

        car[i].printClown2();

    }

}

 

void askWhoToPop(vector<Clown> &car){

int popWho;

    cout<<“Please enter an ID number of a clown to kick out of the car: “;

cin>>popWho;

popThisClown(car, popWho);

cout<<endl;

}

 

void popThisClown(vector<Clown> &car, int popWho){   //how to selectively delete from a vector????

for (int j = 0; j < car.size(); j++)

    {

        cout<<“is “<<j<<” the right guy to bump?”<<endl;

if(car[j].getClownId() == popWho)

        {

            cout<<“yeah, I’m here lookin’ at erasing clown “<<j<<endl;

            //car.erase(j);   //tried this – didn’t work

            car.erase (car.begin()+j);

        }

    }// end for

};

 

output:

**************

vector is size: 0

**************

vector is size: 2

Clown idNumber = 1, name = Pennywise

Clown idNumber = 2, name = John Wayne Gacy

**************

vector is size: 4

Clown idNumber = 1, name = Pennywise

Clown idNumber = 2, name = John Wayne Gacy

Clown idNumber = 3, name = Bozo

Clown idNumber = 4, name = Krusty

**************

current capacity is: 8

vector is size: 5

Clown idNumber = 1, name = Pennywise

Clown idNumber = 2, name = John Wayne Gacy

Clown idNumber = 3, name = Bozo

Clown idNumber = 4, name = Krusty

Clown idNumber = 5, name = Ronald McDonald

Please enter an ID number of a clown to kick out of the car: 3

is 0 the right guy to bump?

is 1 the right guy to bump?

is 2 the right guy to bump?

yeah, I’m here lookin’ at erasing clown 2

is 3 the right guy to bump?

 

**********

current capacity is: 4

vector is size: 5

Clown idNumber = 1, name = Pennywise

Clown idNumber = 2, name = John Wayne Gacy

Clown idNumber = 4, name = Krusty

Clown idNumber = 5, name = Ronald McDonald

Program ended with exit code: 0

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: