Thread: Vectors
View Single Post
Old Apr 17th, 2008, 2:27 PM   #6
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 1 mbd is on a distinguished road
Re: Vectors

you could also use an algorithm and an iterator to print the vector instead of the for loop. then your code would not depend on an random access container.
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

void main() {
	string end = ("no");
	string inser;
	string lst;
	vector<string> names;
	do {
		cout << "Enter as many names as you'd like." << endl;
		cin >> inser;
		names.push_back(inser);
		cout << "Would you like to end?" << endl;
		cin >> end;
		cout << "Would you list to list the names?" << endl;
		cin >> lst;
		if (lst == ("yes")) {
			copy(names.begin(), names.end(), ostream_iterator< string >(cout, "\n");
		}
	}
		while (end != ("yes"));
}
mbd is offline   Reply With Quote