当前位置: 首页 > news >正文

在线网站地图生成器自建购物网站多少钱

在线网站地图生成器,自建购物网站多少钱,怎么查找公司的注册信息,网站做营销推广公司文章目录 1. 计数指针 shared_ptr2. shared_ptr 与函数3. shared_ptr 与 unique_ptr 1. 计数指针 shared_ptr shared_ptr 计数指针又称共享指针,是C11引入的智能指针之一。与unique_ptr不同的是,shared_ptr允许多个指针共享同一块内存。每当一个shared_…

文章目录

  • 1. 计数指针 shared_ptr
  • 2. shared_ptr 与函数
  • 3. shared_ptr 与 unique_ptr

1. 计数指针 shared_ptr

shared_ptr 计数指针又称共享指针,是C++11引入的智能指针之一。与unique_ptr不同的是,shared_ptr允许多个指针共享同一块内存。每当一个shared_ptr 被赋值时,引用计数就会增加。当shraed_ptr被销毁时,引用计数会减小。当引用计数为0时,内存会被自动释放。

shared_ptr内部维护着一个引用计数器,可以通过 use_count() 获取当前引用计数。

代码示例:

#include <iostream>
#include <memory>
using namespace std;int main(int argc, char *argv[]){std::shared_ptr<int> i_p_1 = make_shared<int>(10);cout<< "value: "<< *i_p_1 <<endl;cout<< "use count: "<<i_p_1.use_count() <<endl;// Copystd::shared_ptr<int> i_p_2 = i_p_1;cout<< "value: "<< *i_p_2<<endl;cout<< "i_p_1 use count: " << i_p_1.use_count() << endl;cout<< "i_p_2 use count: " << i_p_2.use_count() << endl;// 修改i_p_2的值*i_p_2 = 30;cout<< "i_p_1 value: " << *i_p_1 << endl;cout<< "i_p_2 value: " << *i_p_2 << endl;// i_p_1 设置为 nullptrstd::shared_ptr<int> i_p_3 = i_p_1;// i_p_1 = nullptr;i_p_1.reset();cout<< "i_p_1 use count: " << i_p_1.use_count() << endl;cout<< "i_p_2 use count: " << i_p_2.use_count() << endl;cout<< "i_p_3 use count: " << i_p_3.use_count() << endl;return 0;
}

代码解析:

  • i_p_1 被初始化为一个 shared_ptr,指向整数10
  • i_p_2 是 i_p_1的副本,因此引用计数会增加
  • 修改了 i_p_2 的值,i_p_1也会变,因为他们共享一块内存
  • 当 i_p_1 被reset() 时,引用计数减少,最终,所有shared_ptr 被销毁时,内存会被自动释放。

2. shared_ptr 与函数

值传入
当 shared_ptr 作为函数参数传递时,默认是值传递(复制传递)。这会增加引用计数,确保指针在函数内部被正确管理。

#include <iostream>
#include <memory>
#include "cat.h"
using namespace std;// 值传入
void cat_by_value(std::shared_ptr<Cat> cat){cout<< cat->get_name() <<endl;cat->set_cat_name("ee");cout<< "func use count :" << cat.use_count() << endl; // 2
}// 引用传入
void cat_by_ref(std::shared_ptr<Cat> &cat){cout<< cat->get_name() <<endl;cat->set_cat_name("ff");cout<< "func use count :" << cat.use_count() << endl;
}// 作为返回值
std::shared_ptr<Cat> get_shared_ptr(){std::shared_ptr<Cat> cat_p = std::make_shared<Cat>("local cat");return cat_p;
}int main(int argc, char *argv[]){std::shared_ptr<Cat> c1 = make_shared<Cat>("dd");cat_by_value(c1);    // 值传递cat_by_ref(c1);         // 引用传递c1->cat_info();cout<< "c1 use_count : " << c1.use_count()<<endl; // 返回值get_shared_ptr()->cat_info();return 0;
}

代码解析:

  • 值传递:当 shared_ptr 作为参数传递时,引用计数增加。函数结束时,局部 shared_ptr 会被销毁,引用计数会减少。
  • 引用传递:使用引用传递时,传递的是原始 shared_ptr,不会增加引用计数
  • 返回值:当 shared_ptr 作为返回值返回时,shared_ptr 会被复制到调用者

3. shared_ptr 与 unique_ptr

  • 不能将 shared_ptr 转化为 unique_ptr
  • unique_ptr 可以转化为 shared_ptr
    • 通过 std::move

常见的设计:

  • 将你的函数返回 unique_ptr 是一种常见的设计模式,这样可以提高代码的复用度,你可以随时改变为 shared_ptr

代码示例:

#include <iostream>
#include <memory>
#include "cat.h"
using namespace std;std::unique_ptr<Cat> get_unique_ptr(){std::unique_ptr<Cat> cat_p = std::make_unique<Cat>("local cat");return cat_p;
}int main(int argc, char *argv[]){std::unique_ptr<Cat> c_p_1 = std::make_unique<Cat>("dd");std::shared_ptr<Cat> c_p_2 = std::move(c_p_1);cout<< "c_p_2.use_count: " <<c_p_2.use_count()<<endl;std::shared_ptr<Cat> c_p_3 = get_unique_ptr(); if(c_p_3){c_p_3->cat_info();cout<< "c_p_3.use_count: "<<c_p_3.use_count()<<endl;}return 0;
}

代码解析:

  • std::move 转换:c_p_1 是一个 unique_ptr,通过 std::move 将其所有权转移给 c_p_2,并转换为 shared_ptr。
  • unique_ptr 到 shared_ptr:返回的 unique_ptr 可以通过 std::move 转换为 shared_ptr,使得多个指针能够共享这块内存
http://www.dtcms.com/a/541018.html

相关文章:

  • 网站开发的整个流程一家只做卫生巾的网站
  • xv6-riscv开发调试环境搭建(vscode+ubuntu)
  • 架构兜底五大手段:构建韧性系统的全面防御体系
  • 阿里巴巴有几个网站是做外贸的wordpress 禅意主题
  • 在 `PaddleOCR` 中配置自定义模型(如自己训练的检测/识别模型)
  • 云谷系统网站开发网站备案要收费吗
  • 前端页面初始化加载速度优化方案
  • 【数据库】金仓数据库构建集团化医院全栈信创“全链路解决方案”
  • MATLAB基于自适应动态特征加权的K-means算法
  • 我的Dify OCR 识别发票准确率测试工具
  • 免费域名申请网站大全封面型网站首页怎么做
  • 专业的网站建设化妆品备案
  • 详解使用CodeBuddy解决高难度项目问题
  • Days.js实时更新时间格式文案在切换全局语言之后的方案
  • 网站icp备案时间网站营销网站营销推广
  • 广州官方宣布网络公司网站优化网站建设
  • 【黑马点评|2 Redis缓存 面试题】
  • 大学学院教授委员会制度研究(二)理论基础与分析框架-杨立恒毕业论文
  • Nginx基础入门篇-基础配置
  • 雅可比SVD算法:高精度矩阵分解的经典方法
  • 在 Python 中测试中assert断言和 if分支的区别
  • 【题解】洛谷 P1169 [ZJOI2007] 棋盘制作 [思维 + dp]
  • 音频限幅器D2761使用手册
  • 网站金融模版wordpress轮播代码
  • 【工具推荐】电脑手机多端互通协作实用
  • 一般网站的跳出率dede做双语网站
  • 自己制作的网站如何发布建筑设计公司经营范围有哪些
  • 51c大模型~合集39
  • 操作【GM3568JHF】FPGA+ARM异构开发板 使用指南:串口
  • 【牛客CM11】链表分割