C++ generic tools -- from C++ Standard Library
今晚学了一下C++标准程序库, 来简单回顾和总结一下。
1.pair 结构体
// defined in <utility> , in the std namespace
namespace std{
template <class T1, class T2>
struct pair{
// type names for the values
typedef T1 first_type;
typedef T2 second_type; // member
T1 first;
T2 second; // default constructor
// implicit invoke the built-in types to provide a default value
pair():first(T1()),second(T2()){
} // constructor for two default values
pair(const T1& a,const T2& b)
: first(a), second(b){
} // copy constructor with implicit conversions,使用模板是因为构造过程中可能需要隐式类型转换。
template<class U,class V>
pair(const pair<U,V>& p)
: first(p.first), second(p.second){
}
}; //comparisions
template <class T1,class T2>
bool operator == (const pair<T1,T2>&, const pair<T1,T2>&);
template <class T1,class T2>
bool operator < (const pair<T1,T2>&,const pair<T1,T2>&);
// similar : != . <, = , >
template <class T1,class T2>
pair<T1,T2> make_pair(const T1&,const T2&);
}
2. make_pair(): 使你无需写出型别,就可以生成一个pair对象.
namespace std{
//create value pair only by providing the values.
template <class T1,class T2>
pair<T1,T2> make_pair(const T1& x,constT2& y){
return pair<T1,T2>(x,y);
}
}
可以使用 std::make_pair(,'@'); 而不用写成: std::make_pair<int,char>(,'@');
3. 智能指针 auto_ptr
为什么要使用智能指针?
如果一开始获取的资源被绑定到局部对象上,当函数退出时,对象的析构函数会被调用, 从而释放资源。 会出现2个常见的问题:
1.经常忘记delete操作
2.当函数出错返回时,可能还没有执行delete
可以使用捕捉所有异常来解决,然后在最后执行delete操作。 但是比较繁琐!不是优良的出现风格,而且复杂易出错。
智能指针的作用: 保证无论在何种情况下, 只要自己被摧毁, 就一定连带释放其所指的资源; 由于智能指针本身就是局部变量,所以无论是正常退出还是异常退出, 只要函数退出,这个局部变量就会被销毁。 这也说明了,局部对象虽然也会摧毁, 但是其绑定的资源仍然没有得到释放。
智能指针的定义: auto_ptr是这样的指针,它是所指向对象的owner!而且是唯一owner, 一个对象只能对应一个智能指针。
智能指针被定义在头文件<memory>
智能指针声明: std::auto_ptr<ClassA> ptr(new ClassA); 使用了auto_ptr 之后就不需要使用catch和delete了。
智能指针的访问: 类似一般指针,有 *, -> 和 = 操作, 但是不允许将普通指针直接赋值给 auto_ptr
智能指针的拥有权关系:
=> 转让: 通过auto_ptr 的copy constructor 来交接拥有权。这种方式使得赋值操作改变了source 变量, 违反了STL对容器元素的要求,所以auto_ptr不允许作为STL的容器元素。
std::auto_ptr<ClassA> ptr1(new ClassA);
std::auto_ptr<ClassA> ptr2(ptr1); // if ptr2 binded another obj, it will be deleted before. then ptr1 is a null pointer!
auto_ptr的用法:(来源于拥有权转让的特殊用法)
(1) 某函数是auto_ptr 的起点: 必须将拥有权传递出去,否则就会在函数退出时被删除, 这里是数据的起点。
(2) 某函数是auto_ptr 的终点: 如果不需要再使用auto_ptr, 就不用传递出去即可, 会被自动删除。
上面是auto_ptr的值传递, 如果是引用传递和const类型的呢?
by reference: 通过reference传递时,无法知道拥有权是否被转移, 所以这种方式的设计不推荐。
by const: 无法变更控制权(但可以修改对象属性), 安全性增强, 在STL中很多接口需要内部拷贝都通过const reference。
使用auto_ptr需要注意:
(1) auto_ptr 对象之间不能共享所有权。
(2) auto_ptr 没有针对array而设计, 因为使用delete , 而没有delete[]。
(3) auto_ptr 只用当拥有对象的智能指针被销毁时, 对象才会被销毁。
(4) auto_ptr 不满足STL容器对元素的要求。
C++ generic tools -- from C++ Standard Library的更多相关文章
- The Python Standard Library
The Python Standard Library¶ While The Python Language Reference describes the exact syntax and sema ...
- [译]The Python Tutorial#11. Brief Tour of the Standard Library — Part II
[译]The Python Tutorial#Brief Tour of the Standard Library - Part II 第二部分介绍更多满足专业编程需求的高级模块,这些模块在小型脚本中 ...
- c++学习书籍推荐《Beyond the C++ Standard Library》下载
百度云及其他网盘下载地址:点我 作者简介 Björn Karlsson works as a Senior Software Engineer at ReadSoft, where he spends ...
- Python语言中对于json数据的编解码——Usage of json a Python standard library
一.概述 1.1 关于JSON数据格式 JSON (JavaScript Object Notation), specified by RFC 7159 (which obsoletes RFC 46 ...
- C++ Standard Library
C++ Standard Library *注:内容主要是对參考1的学习记录.知识点与图片大都来源于该书, 部分知识点与图片来源于參考2. 详细參考信息,见最下方參考. * C++98中新支持的语言特 ...
- C++11新特性——The C++ standard library, 2nd Edition 笔记(一)
前言 这是我阅读<The C++ standard library, 2nd Edition>所做读书笔记的第一篇.这个系列基本上会以一章一篇的节奏来写,少数以C++03为主的章节会和其它 ...
- Python Standard Library
Python Standard Library "We'd like to pretend that 'Fredrik' is a role, but even hundreds of vo ...
- Macro definition of snprintf conflicts with Standard Library function declaration
Macro definition of snprintf conflicts with Standard Library function declaration 即将此处的宏定义注释掉,因为在VS2 ...
- [译]The Python Tutorial#10. Brief Tour of the Standard Library
[译]The Python Tutorial#Brief Tour of the Standard Library 10.1 Operating System Interface os模块为与操作系统 ...
随机推荐
- 30个让人兴奋的视差滚动(Parallax Scrolling)效果网站--转
视差滚动(Parallax Scrolling)是指让多层背景以不同的速度移动,形成立体的运动效果,带来非常出色的视觉体验.作为今年网页设计的热点趋势,越来越多的网站应用了这项技术.今天这篇文章就与大 ...
- 写java代码有感。。。构造方法最好带着,
(一) 小结:具体我最大的担心,害怕就是方法调用的时候,new对象之后,赋值,是在new后面的括号里实现,还是在 对象.方法名()这样的.当然带参数的构造方法,调用时本身就直接调用,普通方法,选后者. ...
- 转:系统吞吐量(TPS)、用户并发量、性能测试概念和公式
PS:下面是性能测试的主要概念和计算公式,记录下: 一.系统吞度量要素: 一个系统的吞度量(承压能力)与request对CPU的消耗.外部接口.IO等等紧密关联. 单个reqeust 对CPU消耗越高 ...
- JoinableQueue
#!/usr/bin/env python # encoding: utf-8 # Date: 2018/6/17import timefrom multiprocessing import Pro ...
- 搭建psdash 监控系统
一.监控系统介绍 Psdash 是一款查看 Linux 系统信息的 web 面板,和另一款系统监控工具 Glances 一样,psDash 的系统信息的采集也是由 psutil 完成的.和 Glanc ...
- angularJS开发碰到的问题
bootstarp css无法加载 http://stackoverflow.com/questions/27656503/how-to-make-yo-angular-load-bootstrap- ...
- 脱壳系列(五) - MEW 壳
先用 PEiD 看一下 MEW 11 1.2 的壳 用 OD 载入程序 按 F8 进行跳转 往下拉 找到这个 retn 指令,并下断点 然后 F9 运行 停在该断点处后再按 F8 右键 -> 分 ...
- 【框架】Spring和dubbox
分布式服务框架 dubbo/dubbox 入门示例 https://www.cnblogs.com/yjmyzz/p/dubbox-demo.html 初识Spring Boot框架 https:// ...
- 七牛云存储的 Javascript Web 前端文件上传
因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,七牛云存储的 Web 前端文件上传 七牛是不错的云存储产品,特别是有免费的配额可 ...
- <a>标签中的href="javascript:;"就是去掉a标签的默认行为
<a>标签中的href="javascript:;"是什么意思? 例子:<a href="javascript:;">我的大学</ ...