泛型算法STL中的迭代器,泛型算法,萃取机的一个实现案例
生活随笔
收集整理的這篇文章主要介紹了
泛型算法STL中的迭代器,泛型算法,萃取机的一个实现案例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
迭代器介紹:
迭代器其實是一個類,但是因為重載了* ++等指針操作的符號,所以有著和指針類似的共能,對于一個自己寫的類,如果想使用迭代器這種功能就得自己寫了,STL的標準容器都有迭代器,迭代器作用于,這樣算法就具有了泛型特點。迭代器可以針對不同的容器進行迭代。
泛型算法
作用于迭代器之上,其具有泛型的特點,實際上是利用非常多的眼花繚亂的模板操作實現的泛型。泛型算法的泛型體現在對不同容器的兼容上。
萃取機
配合泛型算法和迭代器既能針對一個真的指針起效果,又能針對迭代器對象起效果,它是一個中間層,保持了接口的一致性。
//ToDo iterator_traiststemplate<class Iterator>struct iterator_traits{typedef typename Iterator::iterator_category iterator_category;typedef typename Iterator::value_type value_type;typedef typename Iterator::difference_type difference_type;typedef typename Iterator::pointer pointer;typedef typename Iterator::reference reference;};template<class T>struct iterator_traits<T*>{typedef random_access_iterator_tag iterator_category;typedef T value_type;typedef ptrdiff_t difference_type;typedef T* pointer;typedef T& reference;};template<class T>struct iterator_traits<const T*>{typedef random_access_iterator_tag iterator_category;typedef T value_type;typedef ptrdiff_t difference_type;typedef const T* pointer;typedef const T& reference;};//ToDo iteratortemplate<class Category,class Value,class Distance = ptrdiff_t,class Pointer = Value*,class Reference = Value&>struct iterator{typedef Category iterator_category;typedef Value value_type;typedef Distance difference_type;typedef Pointer pointer;typedef Reference reference;};//To design a class and another similar classtemplate<class T>class mList{public:mList(T d):value(d), pNext(NULL),tailor(NULL){}~mList() {}T value;mList* pNext;mList* tailor;void append(T value){if (this->pNext == NULL){this->pNext = new mList(value);tailor = this->pNext;}else{tailor->pNext = new mList(value);tailor = tailor->pNext;}}};template<class T>class mListList{public:mListList(T d) :value(d), pNext(NULL), tailor(NULL) {}~mListList() {}T value;mListList* pNext;mListList* tailor;void append(T value){if (this->pNext == NULL){this->pNext = new mListList(value);tailor = this->pNext;}else{tailor->pNext = new mListList(value);tailor = tailor->pNext;}}};//To design a iterator for the classtemplate<class value,class node>class mListIterator:public iterator<bidirectional_iterator_tag,value>{public:mListIterator(node node) :point(node){};~mListIterator() {};value operator*() const { return point.value; };value& operator++() {point = *(point.pNext); return point.value; }auto end() { return point.pNext;}private:node point;};//to design a algrothom template<class mListIterator>typename iterator_traits<mListIterator>::value_type sum(mListIterator begin){//mListIterator <begin->value,begin,&(*begin)> beginer;typename iterator_traits<mListIterator>::value_type value;value = *begin;while( begin.end() ){++begin;value += *begin;}return value;}to use the algrothom and iterator for the classvoid test_iterator_int(){mList<int> L(1);L.append(2);L.append(3);L.append(4);mListIterator<int, mList<int>> ItBegin(L);auto a = sum<mListIterator<int, mList<int>>>(ItBegin);int b = 1;if (L.pNext != NULL){auto temp = L.pNext;while (temp != NULL){auto temp2 = temp->pNext;delete temp;temp = temp2;}}}void test_iterator_double(){mListList<double> L(1);L.append(2);L.append(3);L.append(4);mListIterator<double, mListList<double>> ItBegin(L);auto a = sum<mListIterator<double, mListList<double>>>(ItBegin);int b = 1;if (L.pNext != NULL){auto temp = L.pNext;while (temp != NULL){auto temp2 = temp->pNext;delete temp;temp = temp2;}}} 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的泛型算法STL中的迭代器,泛型算法,萃取机的一个实现案例的全部內容,希望文章能夠幫你解決所遇到的問題。