STL(1)---从对->运算符的重载到灵巧指针再到迭代器

首先->也就是指针间接引用运算符一定是 一个成员函数,必须返回值是一个对象或者对象的引用,或者必须返回一个指针,被用于选择指针间接引用运算符箭头所指的内容

所谓灵巧指针就是一个对象表现的像一个指针,这时候这个指针比一般的指针具有与生俱来的灵巧性,常称作灵巧指针

所谓迭代器就是一个对指针进行封装的结构,通过对指针封装从而对数据结构中的数据进行访问,从而使指针不直接接触数据,既安全有方便用户

#include <iostream>
#include<vector>
using namespace std;

class Obj//基本的数据类
{
public:
	void f()const
	{
		cout<<i++<<endl;
	}
	void g()const
	{
		cout<<j++<<endl;
	}

private:
	static int i,j;
};

int Obj::i=47;
int Obj::j=11;

class Objcontainer//类的容器
{
public:
	void add(Obj* obj)
	{
		a.push_back(obj);
	}
	friend class SmartPointer;
private:
	vector<Obj*> a;//使用向量类建立一个动态数组obj的对象指针
};
class SmartPointer//灵巧指针
{
private:
	Objcontainer &oc;//容器类的一个对象引用
	int index;//数组索引号
public:
	SmartPointer(Objcontainer &objc):oc(objc),index(0)
	{

	}
	void require(bool b,char*c="ERRO")const
	{
		if(!b)
		{
			cout<<c<<endl;
		}
	}
	bool operator++ ()//负责向后移动这个是对象当作指针的对象
	{
		if(index >= oc.a.size())
			return false;
		if(oc.a[++index]==0)
			return false;
		return true;
	}
	bool operator++(int)
	{
		return operator++();
	}
	Obj* operator->()const //实现对象当作指针
	{
		require((oc.a[index]) != 0,"Zero value");
		cout<<"operator->()\n";
		return oc.a[index];
	}
};

void main()
{
   const int sz =10;
   Obj o[sz];
   Objcontainer oc;
   for(int i=0;i<sz;++i)
   {
	   oc.add(&o[i]);
   }
 SmartPointer sp(oc);
 while(sp++)//调用++重载
 {
	 sp->f();//调用->重载
	 sp->g();

 }
}

上边这个程序就是通过一个类将指针封装,从而使用友员类是各类之间建立联系,从未使用封装指针的类所产生的对象通过内部貌似指针的用法访问数据

下面这个是将封装的指针类内嵌如类中实现灵巧指针

#include <iostream>
#include<vector>
#include <assert.h>
using namespace std;

class obj
{
private:
static int i,j;
public:
	void f()
		{
			cout<<i++<<endl;
		}
		void g()
		{
			cout<<j++<<endl;
		}
};

int obj::i=47;
int obj::j=11;

class objcontainer
{
private:
	vector<obj*>a;
public:
	void add(obj* oj)
	{
		a.push_back(oj);
	}
	class SmartPointer;
	friend SmartPointer;
	class SmartPointer//灵巧指针嵌入类中
	{
	public:
		SmartPointer(objcontainer &oj):oc(oj),index(0)
		{
			cout<<"SmartPointer()\n";
		}
		bool operator++()
		{
			if((index >= oc.a.size())||(oc.a[++index]==0))
				return false;
			return true;
		}
		obj* operator->()const
		{
             assert(oc.a[index] != 0);
			 cout<<"operator->()\n";
			 return oc.a[index];
		}

	private:
		objcontainer &oc;
		size_t index;
	};
	SmartPointer begin()
	{
		return SmartPointer(*this);
	}
};
void main()
{
	const int sz =10;
	obj o[sz];
	objcontainer oc;
	for(int i=0;i<sz;++i)
	{
		oc.add(&o[i]);
	}
	objcontainer::SmartPointer sp = oc.begin();
	while(sp++)//调用++重载
	{
		sp->f();//调用->重载
		sp->g();

	}
}

从这两个代码中是否可以嗅到迭代器的味道??

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2025-01-16 21:21:23

STL(1)---从对->运算符的重载到灵巧指针再到迭代器的相关文章

C++ 运算符的重载

#include <iostream> #include <stdio.h> using namespace std; class Complex //复数类 { public: double real;//实数 double imag;//虚数 Complex(double real=0,double imag=0) { this->real=real; this->imag=imag; } }; Complex operator+(Complex com1,Comp

C++中关于指针运算符-&gt;的重载问题

#include<iostream>using namespace std;struct date{ int year; int month; int day;};struct Person{ string name; int age; bool gender; double salary; date birth; Person() {  cout<<"创建persond对象"<<this<<endl;  age=10; } ~Perso

运算符的重载(复数的相关运算)

运算符的重载实际是一种特殊的函数重载,必须定义一个函数,并告诉C++编译器,当遇到该重载的运算符时调用此函数.这个函数叫做运算符重载函数,通常为类的成员函数.    定义运算符重载函数的一般格式: 返回值类型 类名::operator重载的运算符(参数表) {--} operator是关键字,它与重载的运算符一起构成函数名.因函数名的特殊性,C++编译器可以将这类函数识别出来. 具体的加减乘除等代码运算如下: #include<iostream> #include<string>

对下标运算符[]和函数调用运算符()的重载

一.对下标运算符[]的重载: 重载的格式:返回类型 类名 operator [](参数): #include<iostream> using namespace std; class A { public: A(int n); ~A(); int & operator [](int n);//返回类型为int & 引用类型 private: int *p; int size; }; int & A::operator[](int n){ return p[n];//返回

自增自减运算符的重载(强制类型转换运算符重载)

前置运算符重载为一元运算符,后置运算符重载为二元运算符. Operator int() { return n; } int作为一个强制类型转换运算符被重载, Demo s; (int)s;       //等效于s.int(): 强制类型转换运算符重载时, 不能写返回值类型 实际上其返回值类型----强制类型转换运算符代表的类型 只能作为成员函数,不能作为友元函数或普通函数 转换构造函数和类型转换运算符有一个共同的功能:当需要的时候,编译系统会自动调用这些函数,建立一个无名的临时对象(或临时变量

深入理解运算符的重载

*运算符重载: 1.运算符重载就是赋予已有的运算符多重含义,即多种功能. 2.运算符重载的目的:通过运算符的重载即重新定义使得其能够用于特定类的对象执行特定的功能. 3.对于运算符的重载首先要关心的就是那些运算符能够重载,那些不能重载: 能够重载的运算符: (1).算术运算符:+,-,*,/,%,++,--: (2)位操作运算符:&,|,^,~,<<,>>: (3)逻辑运算符:!,&&,||: (4)比较运算符:>,<,>=,<=,=

【C++】一些基本的运算符的重载

// 实现运算符的重载 #include <iostream> using namespace std; class Int { public: Int(int i = 0) :m(i) { cout << "constructed function" << endl; } ~Int() { cout << "destructor" << endl; } public: Int operator+(cons

结构体类型快速驱魔运算及运算符的重载

下面得到这段代码可以用在很多地方:只需要自己修改下接Ok. 1 struct Matrix 2 { 3 long long mat[N][N]; 4 Matrix operator*(const Matrix m)const//定义矩阵乘法的运算符* 5 { 6 Matrix tmp; 7 for(int i = 0;i < n;i++) 8 { 9 for(int j = 0;j < n;j++) 10 { 11 tmp.mat[i][j] = 0; 12 for(int k = 0;k &

【c++】实现运算符的重载

// 实现运算符的重载 #include <iostream> using namespace std; class Int { public: Int(int i = 0) :m(i) { cout << "constructed function" << endl; } ~Int() { cout << "destructor" << endl; } public: Int operator+(cons