设为首页 加入收藏

TOP

<四>MyVector中加入迭代器功能(一)
2023-07-23 13:34:46 】 浏览:76
Tags:< > MyVector 加入迭

我们之前有<C++模板编程模块>中的第<四>节 理解空间配置器allocator优化STL中的Vector
我将在此基础上加入迭代器功能代码


Iterator 为什么可以遍历所有的容器的方式都一样?
auto it =continer.beign();
for( ;it!=continer.end();++it){
    cout<<*it<<endl;
}

//我们在自己的容器里的 Iterator 的 ++运算 , *运算 由自己来实现,所有对外部使用者来看都是统一的.
//泛型算法能够给所有的容器都使用,也是基于容器对外提供了统一的遍历接口, 其参数接受的都是容器的迭代器


#include <iostream>
using namespace std;


class person {
public:
	//构造函数
	person(int _age=1,char * _pname=nullptr):
	age(_age)
	{
		if (_pname == nullptr) {
			pname = new char[1];
			pname[0] = '\0';
		}
		else {
			int size = strlen(_pname);
			pname = new char[size + 1];
			strcpy(pname, _pname);
		}
		cout << "创建student对象,地址=" << this << endl;
	}
	//拷贝构造函数
	person(const person & _person) {
		this->age = _person.age;
		int size = strlen(_person.pname);
		pname = new char[size + 1];
		strcpy(this->pname, _person.pname);

	}

	//赋值函数
	person & operator=(const person & _person) {
		if (this == &_person) { return *this; }
		delete[]pname;
		pname = nullptr;
		this->age = _person.age;
		int size = strlen(_person.pname);
		pname = new char[size + 1];
		strcpy(this->pname, _person.pname);
		return *this;
	}
	~person() {
		cout << "析构 person =" <<pname << " "<<age << endl;
		delete[]pname;
		pname = nullptr;		
	}

	
private:
	int age;
	char * pname;
	friend ostream & operator<<(ostream & out, const person & _value);
};

ostream & operator<<(ostream & out, const person & _value) {
	cout << _value.pname<<" == "<< _value.age << " "  << endl;
	return out;
}


template <typename T>
class Allocate4 {

public:
	
	//分配内存空间,不创建对象
	T * allocator(int size=4) {
		return (T *)malloc(sizeof(T)*size);
	}
	
	//在指定的内存空间地址,构建 T对象
	void constract(T * pAddress, const T & _val) {
		new (pAddress) T(_val);
	}

	//释放指定位置的内存空间
	void delAllocator(T * pAddress) {
		if (pAddress != nullptr) {
			free(pAddress);
			pAddress = nullptr;
		}
	}

	//析构指定内存位置
	void destory(T * pAddress) {
		pAddress->~T();//调用析构函数
	}

};


template<typename T,typename Allocate= Allocate4<T>>
//类模板
class MyVector4 {

public:

	MyVector4<T,Allocate>(int size = 4 , const Allocate _rallocator = Allocate4<T>)
	: _allocator(_rallocator)
	{
		pfirst = _allocator.allocator(size);
		last = pfirst;
		pend = pfirst + size;
		cout << "MyVector开辟内存地址=" << pfirst<<endl;
	}

	MyVector4<T, Allocate>(const MyVector4<T, Allocate> & _vector)
	{
		//1:根据原vector的空间大小申请新的内存空间
		pfirst = _allocator.allocator(_vector.size());
		last   = pfirst;
		pend   = pfirst + size;

		//2:将原vector空间中的有效对象赋值到新的vector中
		T * _pFlag = _vector.pfirst;
		while (_pFlag != _vector.last) {
			_allocator.constract(last, *_pFlag);
			_pFlag++;
			last++;
		}
	}

	MyVector4<T, Allocate> & operator=(const MyVector4<T, Allocate> & _vector)
	{
		if (this == &_vector) {
			return *this;
		}

		//1:析构现有vector中的有效对象
		T * _pFlag = pfirst;
		while (_pFlag !=last) {
			_allocator.destory(_pFlag);
			_pFlag++;
		}

		//2:释放现有的vector申请的堆内存空间
		_allocator.delAllocator(pfirst);
		pf
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇<七>深入理解new和delete的.. 下一篇easylogging++的那些事(二)宏定义

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目