C++ iterator design pattern

Iterator (iterator) is sometimes called cursor (cursor) is a software design pattern of programming, an interface that can be visited on a container (container, such as a linked list or array), the designer does not need to care about the contents of the container (don't care about the inside of the object) .

The range of the iterator: [) Left closed and right open.
Iterator can also be understood as an object-oriented pointer;
object-oriented pointer: exists in the form of an object, doing pointers;

The difference between iterators and pointers:
containers and strings have iterator types and have members that return iterators. For example, the container has members begin and end, where the begin member returns an iterator pointing to the first element, and the end member returns an iterator pointing to the next position of the last element of the container, which means that end indicates a non-existent Element, so end returns an iterator after the end.

The role of the iterator mode: Iterator mode is also used to solve the problem of traversing an aggregate object, encapsulating the traversal of the aggregation into a
class, so as to avoid the possibility of exposing the internal representation of the aggregate object;

The typical structure diagram of Iterator mode is as follows:
Insert picture description here
From the figure, we can know: The core of Iterator mode is the following four methods:
Insert picture description here
First();
Next();
isDone();
CurrentItem(); The
following codes are respectively
begin()
end()
*it
++ Implementation

Code:

#include<string>//C++ C字符串封装
#include<iostream>
class String;
class Iterator
{
    
    
public:
	Iterator(String* ps, int idx)
		:pstr(ps), index(idx)
	{
    
    }
	bool operator!=(const Iterator rhs)
	{
    
    
		return index != rhs.index;
	}
	const Iterator operator++(int)
	{
    
    
		const Iterator tmp(*this);
		index++;
		return tmp;
	}
	Iterator& operator++()
	{
    
    
		index++;
		return *this;
	}
	char& operator*();
private:
	String* pstr;
	int index;
};
class String
{
    
    
public:
	typedef Iterator iterator;
	String(char* ptr)
	{
    
    
		mptr = new char[strlen(ptr) + 1]();
		strcpy_s(mptr, strlen(ptr) + 1, ptr);
	}
	String(const String& rhs)
	{
    
    
		mptr = new char[strlen(rhs.mptr) + 1]();
		strcpy_s(mptr, strlen(rhs.mptr) + 1, rhs.mptr);
	}
	String& operator=(const String& rhs)
	{
    
    
		if (this != &rhs)
		{
    
    
			delete[] mptr;
			mptr = new char[strlen(rhs.mptr) + 1]();
			strcpy_s(mptr, strlen(rhs.mptr) + 1, rhs.mptr);
		}
		return *this;
	}
	~String()
	{
    
    
		delete[] mptr;
		mptr = NULL;
	}
	iterator begin()
	{
    
    
		return iterator(this, 0);
	}
	//迭代器区间   [)   
	iterator end()
	{
    
    
		return iterator(this, strlen(mptr));//"hello"  5
	}
	char& operator[](int index)
	{
    
    
		return mptr[index];
	}
private:
	char* mptr;
};
//String str("hello"); str[0]// str[1] str[2]
char& Iterator::operator*()
{
    
    
	return (*pstr)[index];
}
#include<vector>//数组
#include<list>
#include<set>//红黑树
int main()
{
    
    
	int arr[] = {
    
     53, 46, 546, 8, 70, 4 };
	int len = sizeof(arr) / sizeof(arr[0]);
	std::vector<int> vector(arr, arr+len);//char*  
	std::vector<int>::iterator it = vec.begin();//it 迭代器   面向对象的指针
	while (it != vec.end())
	{
    
    
		std::cout << *it << " ";//operator*  
		it++;
	}
	std::cout << std::endl;
	return 0;
}

operation result:
Insert picture description here

Guess you like

Origin blog.csdn.net/Gunanhuai/article/details/103425772