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模块为与操作系统 ...
随机推荐
- python学习 (三十五) logging
1 demo import logging # log level: # DEBUG # INFO # Warning # Error # CRITICAL logging.basicConfig( ...
- 【洛谷】P1892 团伙(并查集)+ 求助
题目描述 1920年的芝加哥,出现了一群强盗.如果两个强盗遇上了,那么他们要么是朋友,要么是敌人.而且有一点是肯定的,就是: 我朋友的朋友是我的朋友: 我敌人的敌人也是我的朋友. 两个强盗是同一团伙的 ...
- 修改tomcat端口后不能IP访问问题
当tomcat端口被修改以后使用IP访问会发生404的问题,只能通过localhost进行访问,当别人想访问你tomcat下的工程时就会访问失败,此时修改eclipse下的tomcat中的server ...
- Tkinter Dimensions
Tkinter Dimensions: 各种长度,宽度,和其他部件的尺寸可以在许多不同的单位描述 各种长度,宽度,和其他部件的尺寸可以在许多不同的单位描述. 如果您设置了尺寸为整数,它被假定为 ...
- Android中SharePreferences的简单实现
Android中提供SharePreferences这种轻量级的数据存储模式,这种模式能够存储少量数据,并能为自身和其他应用提供数据接口.相对于其他数据存储方式,SharePreferences更加轻 ...
- C投票系统
- RabbitMQ_direct
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ @version: @author: morgana @licens ...
- [ExtJs6] 环境搭建及创建项目
1. 环境搭建 sencha cmd 和 extjs6 sdk. sencha cmd: https://www.sencha.com/products/extjs/cmd-download/ ext ...
- SqlServer 获得存储过程的返回值(参数返回),别玩了output
declare @dingdanid int declare @fanhuizhi int set @dingdanid = 1 exec 检测订单出库资格 @dingdanid ,@fanhuizh ...
- c#中如何获取本机用户名、MAC地址、IP地址、硬盘ID、CPU序列号、系统名称、物理内存
我们在利用C#开发桌面程序(Winform)程序的时候, 经常需要获取一些跟系统相关的信息, 以下这些代码获取能有些用处. c#中如何获取本机用户名.MAC地址.IP地址.硬盘ID.CPU序列号.系统 ...