#include
#include//C++ string
/*
迭代器:
1.作用:为容器提供统一的遍历方式,再提供遍历的前提下,使得使用人员无法清楚内部的存储结构,加强封装性。
2.面向对象的指针:作为对象却做一个指针在做的事情。
由于字符串类型,物理地址连续,采用下标访问的方式,更快,更方便,更好。所以做了优化。
总结:
迭代器的设计应该由容器开发人员来进行开发,根据不同容器的特点设计不同设计方案的迭代器,必须要有4个函数,
1.开始迭代的函数
2.结束迭代的函数
3.进行迭代的函数,也就是++;
4.访问的函数,也就是*,解引用。
*/
class String;
class Iterator
{
public:
Iterator(String * ptr,int idx)
:pstr(ptr),index(idx)
{ }
bool operator != (const Iterator& rhs)
{
return index != rhs.index;
}
Iterator& operator ++ ()
{
index++;
return *this;
}
Iterator operator ++ (int)
{
Iterator tmp(*this);
index++;
return tmp;
}
char & operator * ();
private:
String * pstr;
int index;
};
class String
{
public:
typedef Iterator iterator;
String(const char * ptr)
{
mptr = new char