#include #include #include "Property.h" #include "House.h" #include "Land.h" using namespace std; using namespace ESTATE; typedef list PropertyList; bool comparator(Property* p1, Property* p2) { return p1->getPrice() < p2->getPrice(); } void using_stl() { int land_size = 10000; int land_price = 200000; int house_price = 600000; int house_size = 2000; // Let's start with 4 Property objects Property *p1 = new Land(land_price++,land_size++); Property *p2 = new House(house_price++,house_size++,land_size++); Property *p3 = new Land(land_price++,land_size++); Property *p4 = new House(house_price++,house_size++,land_size++); // Want to create a list of Property objects PropertyList prop_list; prop_list.push_back(p1); prop_list.push_back(p2); prop_list.push_back(p3); prop_list.push_back(p4); // Now let's print the content of the list cout << "Let's try to print it\n"; PropertyList::const_iterator j; for ( j = prop_list.begin(); j != prop_list.end(); j++) { cout << (*j)->toString2() << endl; } list::const_iterator i; cout << "\n\nContent of the list is " << endl; for ( i = prop_list.begin(); i != prop_list.end(); i++ ) { cout << (*i)->toString2() << endl; } // reverse the list using a generic algorithm // Note that reverse is NOT a member function of list reverse(prop_list.begin(),prop_list.end()); cout << "\n\nContent of the reversed list is " << endl; for ( i = prop_list.begin(); i != prop_list.end(); i++ ) { cout << (*i)->toString2() << endl; } // Sort the list using a specific comparison function // Without the comparison function, the sort simply uses the < operator prop_list.sort(comparator); cout << "\n\nContent of the sorted list is " << endl; for ( i = prop_list.begin(); i != prop_list.end(); i++ ) { cout << (*i)->toString2() << endl; } } int main() { // Example from the slide using a simple data type list my_list; for ( int i = 0; i < 10; i++) { my_list.push_back(i); } //list::const_iterator i; list::iterator i; for ( i = my_list.begin(); i != my_list.end(); i++ ) { cout << "Original element is " << (*i) << endl; (*i) = 10 + (*i) ; } for ( i = my_list.end(); i != my_list.begin(); i-- ) { cout << "Going backwards element is " << (*i) << endl; (*i) = 10 + (*i) ; } cout << "Second time around \n"; for ( i = my_list.begin(); i != my_list.end(); i++ ) { cout << "Element is " << (*i) << endl; } list::reverse_iterator r; for ( r = my_list.rbegin(); r != my_list.rend(); r++ ) { cout << "Reverse iterator element is " << (*r) << endl; } cout << "----------- PART 2 -----------" << endl; // More sophisticated examples using_stl(); return 0; }