STL 迭代器研究 iterator , 自创代码MYList
先来看看链表的实现:
MyList.hpp 代码:
#ifndef __MYLIST__ #define __MYLIST__ #include <assert.h> using namespace std; template<class Ty> struct _Node { _Node() { memset(&data, 0, sizeof(Ty)); prev = 0; next = 0; } _Node(const Ty& val, _Node *p1, _Node *p2) { data = val; prev = p1; next = p2; } Ty data; _Node *prev; _Node *next; }; template<class Ty> class _Itor { public: _Node < Ty > * node;//_Node < Ty > 看成是一个类 ,Node //兼容标准STL typedef size_t size_type; typedef ptrdiff_t difference_type; typedef std::forward_iterator_tag iterator_category; typedef _Node < Ty > value_type; typedef _Node < Ty > * pointer; typedef _Node < Ty > & reference; public: //default construct _Itor() { node = 0; } _Itor(_Node<Ty> *val) { node = val; } //copy construct _Itor(const _Itor<Ty>& _Right) { node = _Right.node; } //= _Itor& operator=(const _Itor<Ty> &_Right) { node = _Right.node; return *this; } // bool operator ==(const _Itor<Ty> &_Right) { return node == _Right.node; } // bool operator != (const _Itor<Ty> &_Right) { return node != _Right.node; } _Itor& operator++() { node = node->next;//很明显用到了容器的方法 return *this; } _Itor& operator--() //e.g. *(--itor) 操作 { node = node->prev; return *this; } _Itor & operator+(int n)//后向偏移个数 { //无法对n 作越界判断 if (n > 0) { while (n--) { node = node->next; } } return *this; } _Itor & operator+=(int n)//后向偏移个数 { //无法对n 作越界判断 if (n > 0) { while (n--) { node = node->next; } } return *this; } _Itor & operator-(int n)//前向偏移个数 { if (n > 0) { while (n--) { node = node->prev; } } return *this; } _Itor & operator-=(int n)//前向偏移个数 { if (n > 0) { while (n--) { node = node->prev; } } return *this; } Ty & operator *()//看来返回引用类型,存取数据皆可 ,* -> 应该返回实际数据 { return node->data; } _Node < Ty > * operator->() { return node; } }; template<class Ty> class MYList { public: typedef _Node<Ty> myNode; typedef _Itor<Ty> myItor;//MYList<Ty> myItor; 等同于 _Itor<Ty> private: myNode *_myHead; size_t _myCount; private: myNode* insert(myNode *pos, myNode *node) { assert(pos != 0 && node != 0); myNode *next = pos->next; pos->next = node; node->prev = pos; next->prev = node; node->next = next; up_count(); return node; } myNode* alloc(const Ty& val) { myNode *tmp = new myNode; tmp->data = val; tmp->prev = tmp->next = 0; return tmp; } myNode *leaveLink(myNode*pos)//返回下一个 { assert(pos != _myHead &&pos != 0); myNode *prev = pos->prev; myNode *next = pos->next; prev->next = next; next->prev = prev; down_count(); delete pos; return next; } void up_count() { _myCount++; } void down_count() { assert(_myCount > 0); _myCount--; } public: myNode * getHead() { return _myHead; } // default construct void clear() { myNode *node = _myHead->next; while (node->next != node) { node = leaveLink(node); } } size_t size() { return _myCount; } bool isEmpty() { return _myCount == 0; } MYList() { _myHead = new myNode; _myHead->next = _myHead->prev = _myHead; _myCount = 0; } ~MYList() { clear(); if (_myHead != NULL) delete _myHead; } MYList(const Ty& val) { _myHead = new myNode; _myHead->next = _myHead->prev = _myHead; _myCount = 0; myNode *tmp = alloc(val); insert(_myHead, tmp); } MYList(const Ty& val, size_t count) { _myHead = new myNode; _myHead->next = _myHead->prev = _myHead; _myCount = 0; for (size_t i = 0; i < count; ++i) { myNode *tmp = alloc(val); insert(_myHead->prev, tmp); //尾部插入 } } //copy construct MYList(MYList< Ty >& val) { _myHead = new myNode; _myHead->next = _myHead->prev = _myHead; _myCount = 0; myNode * head = val.getHead(); myNode *next = head->next; myNode *tmp = NULL; while (next != head) { myNode *tmp = alloc(next->data); insert(_myHead->prev, tmp); //尾部插入 next = next->next; } } Ty & operator[] (size_t i) { assert(i< _myCount); size_t j = 0; myNode *next = _myHead->next; while (j<i) { next = next->next; j++; } return next->data; } // front value Ty& front() { return _myHead->next->data; } // back value Ty& back() { return _myHead->prev->data; } // first itor myItor begin() { return myItor(_myHead->next); } myItor end() { return myItor(_myHead); } void push_front(Ty val) { myNode * tmp = alloc(val); insert(_myHead, tmp); //头部插入 } void push_back(Ty val) { myNode *tmp = alloc(val); insert(_myHead->prev, tmp); //尾部插入 } myNode * pop_front() { return leaveLink(_myHead->next); } myNode * pop_back() { return leaveLink(_myHead->prev); } void resize(size_t _N) { assert(_N<_myCount); if (_N == 0) { clear(); return; } //默认将先push_back()的元素删除 if (_N<_myCount && _N>0) { int nCutCount = _myCount - _N; while (nCutCount--) { pop_front(); } } } MYList<Ty> & operator =(MYList<Ty>& val) { /*_myHead = new myNode; _myHead->next = _myHead->prev = _myHead; _myCount = 0;*/ clear(); myNode * head = val.getHead(); myNode *next = head->next; myNode *tmp = NULL; while (next != head) { myNode *tmp = alloc(next->data); insert(_myHead->prev, tmp); //尾部插入 next = next->next; } return *this; } MYList<Ty> & operator += (MYList<Ty>& val) { myNode * head = val.getHead(); myNode *next = head->next; while (next != head) { myNode *tmp = alloc(next->data); insert(_myHead->prev, tmp); //尾部插入 next = next->next; } return *this; } }; #endif
测试代码:main.cpp
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include "mylist.hpp"
using namespace std;
template <class Ty>
void ShowMyList(MYList<Ty> & tList, char * sListName)
{
MYList<Ty>::myItor itor;
cout << sListName<<" : ";
for (itor = tList.begin(); itor != tList.end(); ++itor)
{
cout << "[" << itor->data << "]" << "-";
}
cout << endl;
}
template <class Ty>
bool FindNullSeat(Ty target)
{
return target == 'c';
}
int _tmain(int argc, _TCHAR* argv[])
{
//for use it
//s0
MYList < char > tempList;
tempList.push_back('A');
tempList.push_back('B');
ShowMyList<char>(tempList,"tempList");
//s1
MYList < char > tempList2('C');
tempList2 += tempList;
ShowMyList<char>(tempList2,"tempList2 += ");
tempList = tempList2;
ShowMyList<char>(tempList, "tempList =");
tempList.pop_front();
ShowMyList<char>(tempList, "tempList pop_front");
cout << "tempList front() =" << tempList.front();
cout << "tempList back() =" << tempList.back();
tempList.push_back('D');
cout << "tempList[2] =" << tempList[2]<<endl;
//s2
MYList < char > tempList3('E', 3);
ShowMyList<char>(tempList3, "tempList3");
MYList < char > tempList4(tempList);
ShowMyList<char>(tempList4, "tempList4");
tempList4.push_front('F');
ShowMyList<char>(tempList4, "tempList4 push_front");
tempList4.pop_back();
ShowMyList<char>(tempList4, "tempList4 pop_back");
tempList4.resize(2);
cout << " tempList4.size()= " << tempList4.size() <<"isEmpty="<< tempList4.isEmpty() << endl;
ShowMyList<char>(tempList4, "tempList4 resize(2)");
tempList4.clear();
cout << " clear then tempList4.size()= " << tempList4.size() << tempList4.isEmpty() << endl;
ShowMyList<char>(tempList4, "tempList4 clear then ");
//s3 iterator
MYList < char > tempList5;
tempList5.push_back('a');
tempList5.push_back('b');
tempList5.push_back('c');
tempList5.push_back('d');
tempList5.push_back('e');
tempList5.push_back('f');
MYList < char >::myItor itor1;
MYList < char >::myItor itor2;
itor1 = tempList5.begin();
itor1 += 3; //'d'
cout << "itor1 += 3;" << " : ";
for (; itor1 != tempList5.end(); ++itor1)
{
cout << "[" << itor1->data << "]" << "-";
}
cout << endl;
itor2 = tempList5.end();
itor2 -= 2; //'e'
cout << "itor1 += 3;" << " : ";
for (; itor2 != tempList5.end(); ++itor2)
{
cout << "[" << itor2->data << "]" << "-";
}
cout << endl;
////////----------
MYList < char >::myItor seat = std::find_if(tempList5.begin(), tempList5.end(), FindNullSeat<char>);//只用用std::find_if()测试通过才能算Iterator编写成功
std::cout << seat->data;
getchar();
return 0;
}
花费点时间,原创之作,转载请注明出处 blogs.cn