// PersonVector Project, File main2.cpp. // Show how to use the STL vector datatype // to store Person objects. #include #include #include #include #include #include "..\\person.h" using namespace std; void print(Person); bool compare(Person, Person); int main() { // Temporary Person object for input. Person p; // STL vector datatype. vector x; vector::iterator y; // Declare and open input file object. ifstream fin("c:\\persons.txt"); // Read data from file and append // to back end of vector. Note that // >> is the overloaded input high level // function. while(fin >> p) x.push_back(p); // 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(); }