一个简单的迭代器的实现。
1 #include <iostream> 2 #include <cassert> 3 using namespace std; 4 5 6 template <typename T> 7 class MyVector 8 { 9 public: 10 class MyIterator 11 {// override * ++ -- = [] + == != 12 13 public: 14 MyIterator():iterHandle(0),iterIndex(0){}; 15 //MyIterator(T* ptr){ 16 // iterHandle = ptr; 17 // iterIndex = 0; 18 //}; 19 ~MyIterator(){}; 20 21 T &operator * () 22 { 23 return *iterHandle; 24 } 25 26 MyIterator& operator++() 27 { 28 iterHandle = iterHandle+1; 29 iterIndex = iterIndex+1; 30 return *this; 31 } 32 33 MyIterator& operator--() 34 { 35 iterHandle = iterHandle-1; 36 iterIndex = iterIndex-1; 37 return *this; 38 } 39 40 MyIterator& operator=(T * otherIt) 41 { 42 iterHandle = otherIt; 43 return *this; 44 } 45 46 MyIterator& operator +(int n) 47 { 48 iterHandle += n; 49 iterIndex += n; 50 return *this; 51 } 52 int index() 53 { 54 return iterIndex; 55 } 56 bool operator==(T * otherIt) 57 { 58 return iterHandle==otherIt; 59 } 60 bool operator!=(T * otherIt) 61 { 62 return iterHandle!=otherIt; 63 } 64 65 private: 66 T * iterHandle; 67 int iterIndex; 68 }; 69 70 MyVector(){ 71 mySize = 1; 72 myData = malloc(sizeof(T)); 73 }; 74 MyVector(int sizeT){ 75 mySize = sizeT; 76 int temp = sizeof(T)*sizeT; 77 myData = (T*)malloc(temp); 78 for(int i=0;i<sizeT;i++) 79 { 80 myData[i] = 0; 81 } 82 83 }; 84 //MyVector(int sizeT); 85 ~MyVector(){ 86 free(myData); 87 myData = NULL; 88 mySize = 0; 89 }; 90 91 T &operator [](int n) 92 { 93 return myData[n]; 94 } 95 T *begin() 96 { 97 return (myData); 98 } 99 T *end() 100 { 101 return (myData+mySize); 102 } 103 int size() 104 { 105 return mySize; 106 } 107 108 void clear() 109 { 110 free(myData); 111 myData = NULL; 112 mySize = 0; 113 } 114 void push_back(const T tempVlue) 115 { 116 T * handleTemp; 117 mySize +=1; 118 handleTemp = (T*)malloc(sizeof(T)*(mySize)); 119 for(int i=0;i<mySize-1;i++) 120 { 121 handleTemp[i] = myData[i]; 122 } 123 handleTemp[mySize-1] = tempVlue; 124 free(myData); 125 myData = handleTemp; 126 handleTemp = NULL; 127 } 128 129 private: 130 T* myData; 131 int mySize; 132 133 };
1 #include "MyVector.h" 2 #include <vector> 3 #include <iterator> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 9 void printT(float x) 10 { 11 cout << x << endl; 12 } 13 int main() 14 { 15 vector<int> vec(3); 16 fill(vec.begin(),vec.end(),3); 17 vector<int>::iterator iter = vec.begin(); 18 19 for(;iter != vec.end();iter++) 20 { 21 cout<<*iter<<endl; 22 } 23 24 cout<<" "<<endl; 25 26 MyVector<float> vec2(3); 27 MyVector<float>::MyIterator iter2; 28 vec2[0] = 2; 29 vec2[1] = 3; 30 vec2[2] = 4; 31 32 iter2=vec2.begin(); 33 for(;iter2!=vec2.end();++iter2) 34 { 35 cout<<*(iter2)<<endl; 36 } 37 for_each(vec2.begin(), vec2.end(),printT); 38 //auto f = [](float x) {cout << x <<endl;}; 39 //for_each(vec2.begin(), vec2.end(), f); 40 //int a = 1; 41 //int b = 2; 42 // 43 //auto f = [a,b](int c){return a+b+c;}; 44 //test(f); 45 return 0; 46 }
输出结果。
1 3 2 3 3 3 4 5 6 2 7 3 8 4 9 2 10 3 11 4 12 13 Process finished with exit code 0
有两个问题:
第一个,不支持lambda表达式,c++完全支持,gcc版本是4.9.2,clion自带cmake版本是3.7.2.
第二个是类型转换的疑惑,begin和end都赋值给了迭代器,有疑惑的是什么时候进行了类型转换。经过大神提示,我自己写的重载operate=自己都没注意到-。-。囧!