Something completely different: vector

You have seen it, and you may want to use it, so here is a complete short description of the "vector" datatype.

What is vector? vector is the answer to all the problems with arrays.

  • It is in the header <vector>

  • In the namespace std

To declare a vector, use the typename vector, and then the contained datatype in pointy brackets <>. Example:

vector<int> bla;    // as opposed to int bla[];
vector<Location *> loc;
vector<string> names;
vector<boolean> isItTrue;
vector<vector<int>> twoDVector;

To add elements to a vector, use its method push_back. push_back takes exactly one argument, which must be of the same type the vector enumerates. Examples:

bla.push_back(1);
loc.push_back(new Location(1,2));
names.push_back("Test");
isItTrue.push_back(true);
twoDVector.push_back(bla); // Probably. Need to test this!

Another important function is "size". Size takes no parameters and returns the number of elements. Example:

vector<int> x;
x.push_back(23);
x.push_back(42);
cout << x.size() << endl; // prints 2

To access elements you can use the [] operator just like with arrays. Just like arrays the first element is at index 0 and the last element is at .size() - 1. Do not access elements out of range! Example:

cout << x[0] << endl; // prints 23
cout << x[1] << endl; // prints 42

You may also write to elements, but only to existing ones!

x[1] = 7; // valid
x[2] = 123; // Will crash. Use .push_back to add elements!

vector has many more fun features. Read C++ in a Nutshell, pg. 722 - 726 if you are interested.

More complete example: The Coin Toss Game again.

// Coin.h

#ifndef COIN_H_
#define COIN_H_

#include <iostream>

enum Side { HEADS, TAILS };
std::ostream& operator<< ( std::ostream &o, Side s);

#endif /*COIN_H_*/
// ComputerPlayer.h

#ifndef COMPUTERPLAYER_H_
#define COMPUTERPLAYER_H_

#include "Player.h"

class ComputerPlayer : public Player
{
private:
	Side bet;
public:
	virtual void makeBet();
	virtual Side getBet() { return bet; }
	ComputerPlayer(std::string name);
};

#endif /*COMPUTERPLAYER_H_*/
// HumanPlayer.h

#ifndef HUMANPLAYER_H_
#define HUMANPLAYER_H_

#include "Player.h"

class HumanPlayer : public Player
{
private:
  Side bet;
public:
	HumanPlayer();
	HumanPlayer(std::string name);
	virtual void makeBet();
	virtual Side getBet() { return bet; }
};

#endif /*HUMANPLAYER_H_*/
// Player.h

#ifndef PLAYER_H_
#define PLAYER_H_

#include "Coin.h"
#include <string>

class Player
{  
protected:
	std::string name;
	Player() {}
public:
	Player(std::string name) { this->name = name; }
	virtual ~Player() {}
	virtual void makeBet()=0;
	virtual Side getBet()=0;
	std::string getName() { return name; }
};

#endif /*PLAYER_H_*/
// TossGame.h

#ifndef TOSSGAME_H_
#define TOSSGAME_H_

#include <vector>

class Player;

class TossGame
{
private:
	std::vector<Player *> players;
public:
	TossGame();
	void play();
	virtual ~TossGame();
};

#endif /*TOSSGAME_H_*/
// Coin.cpp

#include "Coin.h"

std::ostream& operator<< ( std::ostream &o, Side s)
{
	if (s == HEADS)
		o << "heads";
	else
		o << "tails";
	return o;
} 
// ComputerPlayer.cpp

#include "ComputerPlayer.h"
#include <cstdlib>

void ComputerPlayer::makeBet() 
{
	if (rand()%2==0) 
		bet = HEADS;
	else
		bet = TAILS;
}

ComputerPlayer::ComputerPlayer(std::string s) : Player(s)
{
	bet = HEADS;
}
// HumanPlayer.cpp

#include "HumanPlayer.h"
#include <iostream>
using namespace std;

void HumanPlayer::makeBet() 
{
	char c;
	cout << "Would you like to bet on heads (h) or tails (t) ? ";
	cin >> c;
	if (c == 'h') bet = HEADS;
	else if (c == 't') bet = TAILS;
	else {
		cout << "I don't recognize that. I'll just assume tails." << endl;
		bet =TAILS;
	}
}

HumanPlayer::HumanPlayer(string s) : Player(s)
{
	bet = TAILS;
}

HumanPlayer::HumanPlayer()
{
	cout << "What is your name? ";
	cin >> this->name;
}
// main.cpp

#include "TossGame.h"

int main()
{
	TossGame g;
	g.play();
	return 0;
}
// TossGame.cpp

#include "TossGame.h"
#include "HumanPlayer.h"
#include "ComputerPlayer.h"
#include "NetworkPlayer.h"
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

TossGame::TossGame()
{
	srand(time(0));
	//char x;
	//unsigned int number;
	
	/*
	cout << "How many players? "; 
	cin >> number;
	
	for (unsigned i=0;i<number;i++) {
		cout << "What type of player?";
		cin >> 
	}
	*/
	
	players.push_back(new HumanPlayer());
	players.push_back(new ComputerPlayer("Comp"));
	players.push_back(new ComputerPlayer("AnotherComp"));
	//players.push_back(new NetworkPlayer());
}

TossGame::~TossGame()
{
	for (unsigned int i=0;i<players.size();i++) 
		delete players[i];
}

void TossGame::play()
{
	for (unsigned int i=0;i<players.size();i++) {
		cout <<  players[i]->getName() << " makes a bet..." << endl;
		players[i]->makeBet();
		cout << players[i]->getName() << " bets on " << players[i]->getBet() << endl;
	}

	cout << "Now I flip the coin..." << endl;	
	Side landed = TAILS;
	if (rand()%2==0) landed = HEADS;
	cout << "It landed on " << landed << endl;
	
	for (unsigned int i=0;i<players.size();i++) {
		cout << players[i]->getName() << " has ";
		if (players[i]->getBet()==landed) 
			cout << "won!";
		else
			cout << "lost!";
		cout << endl;
	}
}

And the Same without OO:

#include <iostream>
#include <ctime>
#include <cstdio>
#include <vector>
using namespace std;

enum PlayerType { HUMAN, COMPUTER, NETWORK };
enum Side { HEADS, TAILS };

std::ostream& operator<< ( std::ostream &o, Side s)
{
	if (s == HEADS)
		o << "heads";
	else
		o << "tails";
	return o;
} 

struct Player {
	PlayerType playerType;
	string name;
	Side bet;
};

vector<Player *> players;

void initializeGame()
{
	srand(time(0));
	
	Player *m = new Player;
	m->playerType = HUMAN;
	m->name = "Max";
	players.push_back(m);
	
	Player *c = new Player;
	c->playerType = COMPUTER;
	c->name = "Comp";
	players.push_back(c);

	Player *n = new Player;
	n->playerType = NETWORK;
	n->name = "John Doe";
	players.push_back(n);

}

Side betHuman()
{
	Side bet;
	char c;
	cout << "Would you like to bet on heads (h) or tails (t) ? ";
	cin >> c;
	if (c == 'h') bet = HEADS;
	else if (c == 't') bet = TAILS;
	else {
		cout << "I don't recognize that. I'll just assume tails." << endl;
		bet =TAILS;
	}
	return bet;
}

Side betComputer()
{	
	Side bet;
	if (rand()%2==0) 
		bet = HEADS;
	else
		bet = TAILS;
	return bet;
}

void playGame()
{
	for (unsigned int i=0;i<players.size();i++) {
		cout <<  players[i]->name << " makes a bet..." << endl;
		if  (players[i]->playerType == HUMAN )
				players[i]->bet = betHuman();
		else {
				players[i]->bet = betComputer();
		}
		cout << players[i]->name << " bets on " << players[i]->bet << endl;
	}

	cout << "Now I flip the coin..." << endl;	
	Side landed = TAILS;
	if (rand()%2==0) landed = HEADS;
	cout << "It landed on " << landed << endl;
	
	for (unsigned int i=0;i<players.size();i++) {
		cout << players[i]->name << " has ";
		if (players[i]->bet==landed) 
			cout << "won!";
		else
			cout << "lost!";
		cout << endl;
	}
}

void cleanUp()
{	
	for (unsigned int i=0;i<players.size();i++) 
		delete players[i];	
}

int main()
{
	initializeGame();
	playGame();
	cleanUp();
	return 0;
}