2024年C++(5)—,90%的人看完都说好

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

	if (empty())
	{
		return;
	}
	front = (front + 1) % size;
}
int top()//获取队头元素
{
	return _pQue[front];
}
bool full() { return (rear + 1) % size == front; }
bool empty() {
	return front == rear;
}
void resize()
{
	int \*ptmp = new int[size \* 2];
	int index = 0;
	for (int i = front;i != rear;i = (i + 1) % size)
	{
		ptmp[index++] = _pQue[i];
	}
	delete[]_pQue;
	_pQue = ptmp;
	front = 0;
	rear = index;
	size \*= 2;
	ptmp = nullptr;
}
Queue(const Queue &src)
{
	size = src.size;
	front = src.front;
	rear = src.rear;
	_pQue = new int[size];
	for (int i = front;i != rear;i = (i + 1) % size)
		_pQue[i] = src._pQue[i];
}
Queue& operator = (const Queue &src)
{
	if (this == &src)
	{
		return \*this;
	}
	delete[]_pQue;
	size = src.size;
	front = src.front;
	rear = src.rear;
	_pQue = new int[size];
	for (int i = front;i != rear;i = (i + 1) % size)
		_pQue[i] = src._pQue[i];
	return \*this;
}

private:
int *_pQue;//申请队列的数组空间
int front;//对头
int rear;//队尾元素的后继位置
int size;
};
int main()
{
Queue si;
for (int i = 0;i < 20;++i)
{
si.push(rand() % 100);
}
while (!si.empty())
{
cout << si.top() << endl;
si.pop();
}
Queue s2 = si;
si.resize();
getchar();
return 0;
}


### 浅拷贝问题


在动态开辟的数组进行拷贝的时候进行拷贝的时候发生浅拷贝问题,随后在析构函数中调用delete时出现错误。  
 下面手写一个深拷贝的String类



class String
{
public:
String(const char *str = nullptr)
{
if (str != nullptr)
{
m_data = new char[strlen(str) + 1];
strcpy(this->m_data, str);
}
else
{
m_data = new char[1];
*m_data = ‘\0’;
}
}
String(const String &other)
{
m_data = new char[strlen(other.m_data) + 1];
strcpy(this->m_data, other.m_data);
}
~String(void)
{
delete[]m_data;
}
String& operator = (const String &other)
{
//返回this的引用,即返回this本身
if (this == &other)
{
return *this;
}
delete[]m_data;
m_data = new char[strlen(other.m_data) + 1];
strcpy(this->m_data, other.m_data);
return *this;
}
private:
char *m_data;
};
int main()
{
String str1;
String str2(“hello”);
return 0;
}



![img](https://img-blog.csdnimg.cn/img_convert/950ee502662121e8f5b4a8d1bb083966.png)
![img](https://img-blog.csdnimg.cn/img_convert/1115620027e1d263459bce470b818bdf.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618668825)**

C++开发知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618668825)**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值