65,211
社区成员
发帖
与我相关
我的任务
分享
//////这是头文件——Integer.h
#ifndef INTEGER_H
#define INTEGER_H
#include<iostream>
#include<iterator>
#include<utility>
using std::iterator;
using namespace std::rel_ops;
class Integer
:public iterator<std::random_access_iterator_tag,int,int,int*,int>
{
public:
Integer(int avg=0):x(avg){}
Integer(const Integer& y):x(y.x){}
Integer& operator =(const Integer& y)
{
if(this==&y) return *this;
x=y.x;
return *this;
}
~Integer(){}
bool operator ==(const Integer& y)const {return x==y.x;}
bool operator <(const Integer& y)const {return x<y.x;}
int operator *()const {return x;}
int operator [](int n)const {return x+n;}
Integer& operator ++()
{
x++;
return *this;
}
Integer& operator --()
{
x--;
return *this;
}
Integer operator ++(int)
{
Integer tmp(*this);
x++;
return tmp;
}
Integer operator --(int)
{
Integer tmp(*this);
x--;
return tmp;
}
Integer operator+(int n)const
{
return Integer(x+n);
}
Integer operator-(int n)const
{
return Integer(x-n);
}
protected:
int x;
};
#endif
//////这是main函数。
#include<iostream>
#include<algorithm>
#include"Integer.h"
int main()
{
Integer F1(-1);
Integer L1(10);
std::copy(F1,L1,std::ostream_iterator<int>(std::cout," "));
return 0;
}