手写shared_ptr、vector、string

手写vector

#include<iostream>

using namespace std;

template<typename T>
class myvec
{
private:
	T* data;
	int capacity;
	int size;
public:
	myvec()//构造函数
	{
		data = NULL;
		capacity = size = 0;
	}
	myvec(int len)
	{
		data = new T[len];
		capacity = len;
		size = 0;
	}
	myvec(int len,int k)
	{
		data = new T[len*2+1];
		capacity = len*2+1;
		size = len;
		for (int i = 0; i < len; i++)
			data[i] = k;
	}
	myvec(const myvec& tmp)
	{
		if (this == &tmp)
			return;
		data = new T[tmp.capacity];
		capacity = tmp.capacity;
		size = tmp.size;
		for (int i = 0; i < size; ++i)
			data[i] = tmp.data[i];
	}
	myvec<T> operator=(const myvec& a)
	{
		if (this == &a)
			return *this;
		delete[]data;
		data = new T[a.capacity];
		size = a.size;
		capacity = a.capacity;
		for (int i = 0; i < size; i++)
			data[i] = a.data[i];
		return *this;
	}

	T& operator[](int index)
	{
		if (index >= size)
			cout << "Overflow!" << endl;
		return data[index];
	}

	const myvec& push_back(const T temp)
	{
		if (size >= capacity)
		{
			T* newdata = new T[capacity * 2 + 1];//cap=0的时候需要加1才扩容
			memcpy(newdata, data, capacity * sizeof(T));
			delete[]data;
			data = newdata;
			capacity = 2 * capacity + 1;
		}
		data[size++] = temp;
		return *this;
	}

	const int getsize() {
		return size;
	}

	const int getcapaticy()
	{
		return capacity;
	}

	~myvec()
	{
		delete []data;
	}

	void pop_back()
	{
		if (size <= 0)
		{
			cout << "error!" << endl;
			return;
		}
		size--;
	}

};


int main()
{
	myvec<int> vest;
	myvec<int> a(10,1);
	myvec<int> v (a);
	myvec<int> v = a;

	return 0;

}

手写string

#include<iostream>
#include<cstring>

using namespace std;

class String
{
public:
	char* data;
	int size;

	String(const char* str)
	{
		if (str == NULL)
		{
			data = new char[1];
			data[0] = '\0';
			size = 0;
		}
		else
		{
			size = strlen(str);
			data = new char[size + 1];
			strcpy_s(data, strlen(str) + 1,str);
			//strcpy安全版本。strcpy已在vs里废弃
		}
	}

	String(const String& str)
	{
		size = str.size;
		data = new char[size + 1];
		strcpy_s(data, str.size + 1,str.data);
	}

	String& operator=(const String &str)
	{
			if (this == &str)
				return *this;
			delete[]data;
			size = str.size;
			data = new char[size + 1];
			strcpy_s(data, str.size + 1,str.data);
			return *this;
	}
	
	~String()
	{
		delete[]data;
	}
	
};

int main()
{
	String s("this is my string");
	String b(s);
	String c = s;
	cout << s.data << endl;
	cout << b.data << endl;
	cout << c.data << endl;
	return 0;
}

手写shared_ptr

#include <iostream>
using namespace std;

template<typename T>
class mshared_ptr {
public:
	T* ptr;
	int *count;
	mshared_ptr(T *_ptr) 
	{
		ptr = _ptr;
		count = new int(0);
		if (ptr != NULL)
			*count = 1;
	}
	
	mshared_ptr(const mshared_ptr &r)
	{
		cout << "拷贝构造" << endl;
		ptr = r.ptr;//让两个指针指向同一个地方
		count = r.count;//让count指针和原来的指向同一个地方
		++*r.count;//原来的对象的引用计数++
	}
	
	mshared_ptr &operator=(const mshared_ptr &r)
	{
		if (--*count == 0)
		{
			delete ptr;
			delete count;
			cout << "赋值重载中释放内存" << endl;
		}
		ptr = r.ptr;
		count = r.count;
		++*r.count;
		return *this;
	}
	
	T operator*()
	{
		return *ptr;
	}
	
	~mshared_ptr()
	{
		if (--*count == 0)
		{
			delete ptr;
			delete count;
		}
	}
	//引用计数
	int use_count()
	{
		return *count;
	}
};

int main(void)
{
	mshared_ptr<double> ptr(new double(3.14));
	cout << *ptr << endl;
	// {
	mshared_ptr<double> ptr1 = ptr;
	cout << *ptr << endl;
	cout << *ptr1 << endl;
	cout << "引用计数是" << ptr.use_count() <<
		"\t" << ptr1.use_count() << endl;
	// }
	// cout<<"ptr1删除后ptr引用计数是"<<ptr.use_count()<<endl;

	mshared_ptr<double> ptr_n1(new double(1.59));
	ptr_n1 = ptr;/*在让ptr_n1指向新的空间之前,ptr_n1原来的空间的
	引用计数要-1,如果为0要清除*/
	cout << "赋值给ptr_n1后引用计数是" << ptr.use_count() << endl;

}



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值