class MyIterator
{
public:
MyIterator(int* vector)
{
m_vector = vector;
}
int& operator *()
{
return *m_vector;
}
bool operator!=(const MyIterator it)
{
return m_vector != it.m_vector;
}
MyIterator& operator++()
{
m_vector++;
return *this;
}
MyIterator operator++(int)
{
MyIterator tmp = *this;
++(*this);
return tmp;
}
private:
int* m_vector;
};
class MyVector
{
public:
typedef MyIterator iterator;
MyVector(int n)
{
m_count = n;
m_vector = new int[n];
for (int i = 0; i < n; ++i)
{
m_vector[i] = i;
}
}
iterator begin()
{
return iterator(m_vector);
}
iterator end()
{
return iterator(m_vector + m_count);
}
private:
int* m_vector;
int m_count;
};
int main()
{
MyVector mv(5);
//MyVector::iterator it = mv.begin();
//while (it != mv.end())
//{
// cout << *(it++) << endl;
//}
const auto& begin = mv.begin();
const auto& end = mv.end();
std::for_each(begin, end, [](const auto e)
{
std::cout << e << endl;
});
return 0;
}
参考网址:https://blog.csdn.net/XiaoHeiBlack/article/details/77014626
版权声明:本文为wodownload2原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。