C C++最全C++(17)——泛型算法绑定器和智能指针_c+(1),2024年C C++大厂面试

img
img

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

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

如果你需要这些资料,可以戳这里获取

//for\_each 可以遍历容器的所有元素,可以自行添加合适的函数对象对容器进行过滤
for\_each(vec.begin(),vec.end(),
	[](int val)->void
{
	if (val % 2 == 0)
	{
		cout << val << " ";
	}
});

cout << endl;
return 0;

}


### 智能指针


将所有智能指针进行仿写,然后指明存在问题



智能指针
 C++98 auto_ptr
 C++11 unique_ptr
		shared_ptr
		weak_ptr
为了防止内存泄露,只能指针具有所有权唯一的特性

auto_ptr 所有权转移
smart_ptr  释放权转移 在外部函数中被赋值运算符重载
unique_ptr 所有权唯一,不允许权限转移 禁止拷贝构造和赋值运算符重载
shared_ptr  带引用计数的只能指针,强智能指针,类对象里面含有智能指针相互指向的时候会出现析构错误
weak_ptr 弱智能指针,解决强智能指针相互引用的问题  不添加引用计数、不能单独使用
\*/

template
class Auto_ptr//赋值或者拷贝构造后先定义的对象会出错
{
public:
Auto_ptr(T* ptr)
:mptr(ptr) {}
~Auto_ptr()
{
delete mptr;
}
Auto_ptr(Auto_ptr& rhs)
{
mptr = rhs.Release();
}
Auto_ptr operator=(Auto_ptr& rhs)
{
if (this != &rhs)
{
delete mptr;
mptr = rhs.Release();
}
return *this;
}
T* operator->()
{
return mptr;
}
T& operator*()
{
return *mptr;
}

private:
T* Release()
{
T* tmp = mptr;
mptr = NULL;
return tmp;
}
T* mptr;
};

//带标志位的智能指针:新的智能指针指向后获得释放权,旧指针失去释放权
template
class Smart_ptr
{
public:
Smart_ptr(T*ptr)
:mptr(ptr), flag(true)
{}
~Smart_ptr()
{
if (flag)
{
delete mptr;
}
mptr = NULL;
}
Smart_ptr( Smart_ptr& rhs)
{
mptr = rhs.mptr;
flag = rhs.flag;
rhs.flag = false;
}
Smart_ptr& operator=( Smart_ptr& rhs)
{
if (this != &rhs)
{
this->~Smart_ptr();
mptr = rhs.mptr;
flag = rhs.flag;
rhs.flag = false;
}
return *this;
}
T* operator->()
{
return mptr;
}
T& operator*()
{
return *mptr;
}
private:
T *mptr;
int flag; //释放权标志位
};
Smart_ptrgetobject(Smart_ptr& arg)
{
Smart_ptr tmp = arg;
return tmp;
}

template
class Unique_ptr
{
public:
Unique_ptr(T* ptr)
:mptr(ptr)
{}
~Unique_ptr()
{
delete mptr;
}
T* operator->()
{
return mptr;
}
T& operator*()
{
return *mptr;
}
private:
// Unique_ptr(const Unique&);
// Unique_ptr& operator=(const Unique_ptr&);
T* mptr;

};

//shared_ptr

class number
{
public:
number()
{
curindex = 0;
}
void addRef(void* ptr)
{
int index = FindIndex(ptr);
if (index < 0)
{
node[curindex].addr = ptr;
node[curindex].count = 1;
curindex++;
}
else
{
node[index].count++;
}
}
void delRef(void* ptr)
{
int index = FindIndex(ptr);
if (index == -1 && ptr == nullptr)
{
throw exception(“ref is errpr”);
}
else
{
if (getRef(ptr) > 0)
{
node[index].count–;
}
}
}
int getRef(void* ptr)
{
int index = FindIndex(ptr);
if (index < 0)
{
return -1;
}
else
{
return node[index].count;
}
}
private:
int FindIndex(void* ptr)
{
for (int i = 0;i < 10;i++)
{
if (node[i].addr == ptr)
{
return i;
}

	}
	return -1;
}
class Node
{
public:
	Node(void\* add = NULL, int cnt = 0)
		:addr(add), count(cnt)
	{}
public:
	void\* addr;
	int count;
};
Node node[10];
int curindex;//结构中有效元素个数

};
template
class Shared_ptr
{
public:
Shared_ptr(T* ptr = nullptr)
:mptr(ptr)
{
num.addRef(mptr);
}
Shared_ptr(const Shared_ptr& rhs)
{
mptr = rhs.mptr;
num.addRef(mptr);
}
Shared_ptr operator=(const Shared_ptr& rhs)
{
if (this == &rhs)
{
return *this;
}
num.delRef(mptr);
if (num.getRef(mptr) == 0)
{
delete mptr;
}
mptr = rhs.mptr;
num.addRef(mptr);
}
~Shared_ptr()
{
num.delRef(mptr);
if (num.getRef(mptr) == 0)
{
delete mptr;
}
mptr == nullptr;
}

img
img

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

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

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

833)]
[外链图片转存中…(img-1MKKYwyu-1715681939833)]

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

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

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

  • 15
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值