00001
00017 #include <list>
00018 #include <iostream>
00019 #include <string>
00020 #include <new>
00021 using namespace std;
00022
00023
00024 typedef list<string*, allocator<string*> > STRING_PTR;
00025
00026 namespace std
00027 {
00028
00029 template<> struct greater<string*>
00030 {
00031
00032 bool operator() (string* s1, string *s2)
00033 {
00034 return (*s1) > (*s2);
00035 }
00036 };
00037 }
00038
00039 int main()
00040 {
00041
00042 STRING_PTR groceryList;
00043 STRING_PTR::iterator i;
00044
00045
00046
00047 try
00048 {
00049 string* str = new string ("apples");
00050 groceryList.insert(groceryList.end(), str);
00051 str = new string ("bread");
00052 groceryList.insert(groceryList.end(), str);
00053 str = new string ("juice");
00054 groceryList.insert(groceryList.end(), str);
00055 str = new string ("carrots");
00056 groceryList.insert(groceryList.end(), str);
00057
00058 cout << "Number of items on my grocery list: "
00059 << groceryList.size() << endl;
00060
00061
00062 groceryList.sort(greater<string*>());
00063
00064
00065 cout << "Items after the predicate sort are:" << endl;
00066 i = groceryList.begin();
00067 while (i != groceryList.end())
00068 { cout << (*(*i)).c_str() << endl;
00069 i++;
00070 }
00071 }
00072 catch (bad_alloc e)
00073 { cout << e.what() << endl;
00074 }
00075 }