const 引用了解一下#include <iostream>
#include <vector>
using namespace std;
class arr_iterator {
private:
int index;
public:
arr_iterator(int x){index = x;}
arr_iterator& operator ++(){
index++;
return *this;
}
arr_iterator& operator ++(int){
index++;
return *this;
}
int operator * ();
int get(){return index;}
//bool operator !=(arr_iterator &x2){
bool operator !=(const arr_iterator &x2){
if(index != x2.index) return true;
else return false;
}
};
class arr{
private:
int _begin;
int length;
public:
static vector<int> s;
typedef arr_iterator iterator;
friend arr_iterator;
arr(){
_begin = 0;
length = 9;
}
arr_iterator begin(){
return arr_iterator(_begin);
}
arr_iterator end(){
return arr_iterator(_begin+length);
};
};
vector<int> arr::s = {1,2,3,4,5,6,7,8,9};
inline int arr_iterator::operator * (){
return arr::s[this->index];
}
int main(){
arr x = arr();
auto begin = x.begin();
auto end = x.end();
/* for(;begin!=end;begin++){
cout << *begin << endl; 我这样先获取头和尾的iterator 再判断是可以的
}
*/
for(auto i = x.begin();i!=x.end();i++){ //但是我这样 他就说我没有对!=进行重载
cout << *i << endl;
}
}
|