Sie sind auf Seite 1von 4

list.

cpp ///////////////////////// // Class item<T> ///////////////////////// template<class T> class item { T* data; item* prev; item* next; public: item(T* pD = NULL) : data(pD), prev(NULL), next(NULL) {} ~item() { delete data; delete next; // how does that work? } // no implementation for C.C. and assignment item* GetPrev() { return prev; } item* GetNext() { return next; } T* GetData() { return data; } void SetPrev(item* pt) { prev = pt; } void SetNext(item* pt) { next = pt; } void SetData(T* pd) { data = pd; } }; //////////////////////// // Class list<T> //////////////////////// template<class T> class list { item<T>* head; item<T>* tail; public: list() : head(new item<T>), tail(new item<T>) { head->SetNext(tail); tail->SetPrev(head); } ~list() {delete head;} // how does that work? //////////////////////////////// // inner class: iterator //////////////////////////////// class iterator { item<T>* pCurrent; friend class list<T>; public: iterator(item<T>* pt = 0) : pCurrent(pt) {} int operator!=(iterator itr) const { return (pCurrent != itr.pCurrent); }

list.cpp cont > iterator operator++() throw(char*) > { > if(pCurrent == NULL) > throw "null pointer in list::iterator"; > pCurrent = pCurrent->GetNext(); > return *this; > } > > iterator operator--() throw (char*) > {} > > T& operator*() throw (char*) > { > if(pCurrent == NULL || pCurrent->GetData() == NULL) > throw "null pointer in list::iterator"; > return *(pCurrent->GetData()); > } > > operator T*() > { > return (pCurrent ? pCurrent->GetData() : NULL); > } > }; > > // continue with class list... > iterator insert(iterator itr, const T& data) > { > // create a copy of data > item<T>* pNewItem = new item<T>(new T(data)); > > // connect > pNewItem->SetNext(itr.pCurrent); > pNewItem->SetPrev(itr.pCurrent->GetPrev()); > itr.pCurrent->GetPrev()->SetNext(pNewItem); > itr.pCurrent->SetPrev(pNewItem); > > return pNewItem; // How does that work? > } > > iterator erase(iterator itr) > { > // tie together > item<T>* temp = itr.pCurrent->GetNext(); > itr.pCurrent->GetPrev()->SetNext(temp); > temp->SetPrev(itr.pCurrent->GetPrev()); > > // to avoid deletion of list > itr.pCurrent->SetNext(NULL); > delete itr.pCurrent; > return temp; > } > > iterator begin() { return head->GetNext() ; } > iterator end() { return tail; } > > };

find.cpp template<class iterator, class T> inline iterator Find(iterator begin, iterator end, const T& value) { while (begin != end && *begin != value) begin++; return begin; } main1.cpp #include "list.cpp" #include "find.cpp" #include <iostream> #include <string> using namespace std; void main() { list<int> iList; for (int i = 1; i < 10 ; i++) iList.insert(iList.end(), i); int value = 8; list<int>::iterator found; found = Find(iList.begin(), iList.end(), value); if (found != iList.end()) cout<<"FOUND"<<endl; // Use found to delete the item iList.erase(found); // and print list<int>::iterator itr; for (itr = iList.begin() ; itr != iList.end() ; itr++) cout<<*itr; // list works for any type supporting ==, C.C. etc. list<string> sList; sList.insert(sList.end(), "This"); sList.insert(sList.end(), "is"); sList.insert(sList.end(), "a"); sList.insert(sList.end(), "beautiful"); sList.insert(sList.end(), "day"); list<string>::iterator sfound; sfound = Find(sList.begin(), sList.end(), "is"); sList.erase(sfound); list<string>::iterator sitr; for (sitr=sList.begin() ; sitr!=sList.end() ; sitr++) cout<<*sitr; // Find still works on arrays int array[ ] = {3,2,5,7,2,8,11}; int size=sizeof(array)/sizeof(array[0]); int* ifound; ifound = Find (&array[0], &array[size], 7); }

apply.cpp #include <math.h> #include <algorithm> template<class iterator, class operation> inline void Apply(iterator begin, iterator end, const operation& f) { for( ; begin != end ; ++begin) f(*begin); } struct Sqr { template<class T> void operator()(T& number) const { number = number * number; } }; struct Print { template<class T> void operator()(T& printable) const { cout<<printable<<endl; } }; struct Reverse { template<class T> void operator()(T& reversable) const { reverse(reversable.begin(), reversable.end()); } }; inline void Sqrt(double& number) {number = sqrt(number); }

main2.cpp #include "list.cpp" #include "find.cpp" #include "apply.cpp" #include <iostream> #include <string> #include <algorithm> using namespace std; void main() { Sqr MySqrMachine; Print MyPrintMachine; int i = 2; float f = 3.25; MySqrMachine(i); MySqrMachine(f); MyPrintMachine(i); // and now - use the list list<double> fList; for (i = 1; i < 10 ; i++) fList.insert(fList.end(), i/10.0); Apply(fList.begin(), fList.end(), MyPrintMachine); // or... Apply(fList.begin(), fList.end(), Print()); Apply(fList.begin(), fList.end(), Sqr()); Apply(fList.begin(), fList.end(), Print()); Apply(fList.begin(), fList.end(), Sqrt); Apply(fList.begin(), fList.end(), Print()); // And for a list of strings list<string> sList; sList.insert(sList.end(), "sihT"); sList.insert(sList.end(), "si"); sList.insert(sList.end(), "a"); sList.insert(sList.end(), "yad"); Apply(sList.begin(), sList.end(), Print()); // Reverse the words Apply(sList.begin(), sList.end(), Reverse()); Apply(sList.begin(), sList.end(), Print()); }

stl1.cpp #include <iostream> #include <string> #include <vector> #include <functional> #include <algorithm> using namespace std; // return true is the input stirng starts with S int MatchFirstChar(const string& str) { return (str[0] == 'S'); } void main() { const int VECTOR_SIZE = 8; typedef vector<string> sVector; typedef sVector::iterator sVectorItr; // define the objects sVector Names(VECTOR_SIZE); sVectorItr start, end, itr; int result = 0; // Init Names[0] = "She"; Names[1] = "Sells"; Names[2] = "Sea"; Names[3] = "Shealls"; Names[4] = "by"; Names[5] = "the"; Names[6] = "Sea"; Names[7] = "Shore"; start = Names.begin(); end = Names.end(); // Print Names list cout<<"Names: {"; for (itr = start ; itr != end; itr++) cout<<*itr<<" "; cout<<" }"<<endl; // count the nuber of elemennts starting with S result = count_if(start, end, MatchFirstChar); cout<<"Number of names starts with S = "<<result<<endl; }

stl2.cpp #include <iostream> #include <string> #include <vector> #include <list> #include <deque> #include <algorithm> using namespace std; void main() { const int MAX_ELEMENTS = 8; // INTEGER VECTOR typedef vector<int> iVector; typedef iVector::iterator iVectorItr; iVector Numbers_v(MAX_ELEMENTS); iVectorItr start_v, end_v, itr_v; // INTEGER LIST typedef list<int> iList; typedef iList::iterator iListItr; iList Numbers_l; iListItr start_l, end_l, itr_l; // INTEGER QUEUE typedef deque<int> iDeque; typedef iDeque::iterator iDequeItr; iDeque Numbers_q(2 * MAX_ELEMENTS); iDequeItr itr_q, start_q = Numbers_q.begin(); // INIT VECTOR AND LIST Numbers_v[0] = 4; Numbers_v[1] = 14; Numbers_v[2] = 67; Numbers_v[3] = 8; Numbers_v[4] = 10; Numbers_v[5] = 123; Numbers_v[6] = 7; Numbers_v[7] = 16; for (int i = 0; i < MAX_ELEMENTS ; i++) Numbers_l.push_back(i); // ASSIGN VECTOR/ LIST BEGIN AND END start_v = Numbers_v.begin(); end_v = Numbers_v.end(); start_l = Numbers_l.begin(); end_l = Numbers_l.end(); // SORT THE VECTOR sort(start_v, end_v);

stl2.cpp cont > > // PRINT THE VECTOR AND LIST > cout<<"Numbers Vector { "; > for (itr_v = start_v ; itr_v != end_v ; itr_v++) > cout<<*itr_v<<" "; > cout<<"}"<<endl; > > cout<<"Numbers List { "; > for (itr_l = start_l ; itr_l != end_l ; itr_l++) > cout<<*itr_l<<" "; > cout<<"}"<<endl; > > // AND NOW MERGE > merge(start_v, end_v, start_l, end_l, start_q); > > // AND PRINT THE MERGED QUEUE > cout<<"Numbers Deque (merged) { "; > for (itr_q = Numbers_q.begin() ; itr_q != Numbers_q.end() ; > itr_q++) > cout<<*itr_q<<" "; > cout<<"}"<<endl; > } > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >

Das könnte Ihnen auch gefallen