1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 泛型算法STL中的迭代器 泛型算法 萃取机的一个实现案例

泛型算法STL中的迭代器 泛型算法 萃取机的一个实现案例

时间:2022-05-21 04:50:36

相关推荐

泛型算法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_typedifference_type;typedef typename Iterator::pointer pointer;typedef typename Iterator::reference reference;};template<class T>struct iterator_traits<T*>{typedef random_access_iterator_tagiterator_category;typedef Tvalue_type;typedef ptrdiff_t difference_type;typedef T* pointer;typedef T& reference;};template<class T>struct iterator_traits<const T*>{typedef random_access_iterator_tagiterator_category;typedef Tvalue_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 Pointerpointer;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;}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。