// PersonVector Project, File main1.cpp. // Show how to use the STL vector datatype // to store pointers to Person objects. #include #include #include #include #include #include "..\\person.h" using namespace std; void print(Person *); bool compare(Person *, Person *); int main() { // STL vector datatype storing // pointers to Person objects. vector x; vector::iterator y; // Varibles for input. string n; char g; int a; // Declare and open input object. ifstream fin("c:\\persons.txt"); // Read data from file and append // to back end of vector. while(fin >> n >> g >> a) x.push_back(new Person(n, g, a)); // Sort using high level sort function. sort(x.begin(), x.end(), compare); // Print using for_each construction. for_each(x.begin(), x.end(), print); // Print by accessing each object individually. for(int i = 0; i < x.size(); i++) cout << x[i] -> get_name() << " "; cout << endl; // Print using an iterator. for(y = x.begin(); y != x.end(); y++) cout << (*y) -> get_name() << " "; cout << endl; return EXIT_SUCCESS; } void print(Person *p) { p -> display(); } bool compare(Person *p, Person *q) { return p -> get_age() < q -> get_age(); }