Thread: Vectors
View Single Post
Old Apr 19th, 2008, 11:31 PM   #13
White-Hat`
Programmer
 
Join Date: Sep 2005
Location: Oopland
Posts: 36
Rep Power: 0 White-Hat` is on a distinguished road
Re: Vectors

Alright, I have a question.

When using the...
copy(names.begin(), names.end(), ostream_iterator<string>(cout, "\n"));
algorithm suggested to me, how can I apply this to my own class and member functions?

I have a class called CAddressBook and I am using a member function named print(), which prints names, addresses, etc. My first thoughts were to write a for loop incrementing an integer until all the objects were printed, but I want to break this habit.

EXAMPLE:
copy(names.begin(), names.end(), ostream_iterator<vector/*or CAddressBook?*/>(print(), "\n"));
(I know that wouldn't work, just wanted to help pass a little more clarification along if something wasn't being understood.

Any help is appreciated.

Edit: This is what I have so far.. going to sleep for the night. If anyone has any addition comments, they're welcome.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class CAddressBook {
private:
	string name;
	string address;
	string pn;	//phone number
	string on;	//other notes
public:
	CAddressBook(string, string, string, string);
	~CAddressBook();
	void CABoutput(void);
};
CAddressBook(string a, string b, string c, string d) {
	name = a;
	address = b;
	pn = c;
	on = d;
}
~CAddressBook() {
	cout << "Object destroyed";
}
void CABoutput(void) {
	cout << "Name: " << name << "\nAddress: " << address << "\nPhone Number: " << pn << "\nOther Notes: " << on;
	cout << endl << endl;
}
int main() {
	size_t size = 100;
	vector<CAddressBook*> a_b(size);
	string end;
	int choose;
	int i = 0;
	cout << "Welcome to your address book!\n";
	while(end != ("quit")) {
		string name = "Jon Doe";
		string address = "None entered";
		string pn = "None entered";
		string on = "None entered";

		cout << "Would you like to...>\n1.) Enter a new entry\n2.) List current entries\n3.) Quit\n" << 
			"Please enter your choise by number.\n";
		cin >> choose;
		if (choose == 1) {
			cout << "Enter name: ";
			cin >> name;
			cout << "\nEnter address: ";
			cin >> address;
			cout << "\nEnter phone number: ";
			cin >> pn;
			cout << "\nEnter other notes: ";
			cin >> on;
			cout << endl;
			a_b[i] = new CAddressBook(name, address, pn, on);
			i++;
		}
		else if (choose == 2) { //this is where I want to include the algorithm
White-Hat` is offline   Reply With Quote