c++标准库一周一记(1)
2012-03-08 23:46
204 查看
一,Pairs
有点类似java的map,但我更想说这不是xml中的key和value吗!!!
pair 定义于 <utility>头文件,可以使用如: std::pair<int, float> p 或者
std::make_pair(int ,float) 的方法来使用pair. 这里有个例子来使用make_pair:
ex:
void f(std::pair<int, const char*>);
void g(std::pair<const int, std::string>);
...
void foo {
f(std::make_pair(42, "Hello"));
g(std::make_pair(24, "Hello"));
//with type conversions
}
其中可以看出make_pair() 使得“将两个值当做一个pair参数来传递的动作更容易记住。”
二、智能指针(auto_ptr)
auto_ptr 为了在程序中new 的对象资源,在程序结束时或需要释放资源时,没有释放(就是没有delete )。auto_ptr规定它是“它所指向的对象”的拥有者,所以当auto_ptr被摧毁时,该对象也将遭到摧毁。它要求一个对象只能拥有一个拥有者,严禁一女二夫。
可以看到auto_ptr 在解决了资源释放问题后,又引入了一个新的问题:“绝对不能以同一个对象为初值,将两个auto_ptr初始化”,这个问题多发生于该auto_ptr赋值。为了解决这个问题,令auto_ptr的copy构造函数和assignment函数将对象拥有权交出去。
如: //initialize an auto_ptr with a new object
std::auto_ptr<classA> ptr1(new classA );
//copy the auto_ptr
std::auto_ptr<classA> ptr2(ptr1);
现在,ptr1就没有那个new出来的对象的拥有权,而在ptr2上。
注:只有auto_ptr 可以拿来当做另一个auto_ptr的初值,普通指针是不行的。
使用地方:使得auto_ptr作为成员之一,在class 中使用,就可以避免遗失资源。
错误运用:
1、auto_ptr 之间不能共享拥有权。
2、并不存在针对array而设计的auto_ptr。
3、auto_ptr 绝非一个四海通用的智能指针。
4、auto_ptr 不满足STL容器对其元素的要求。
ex:
std::auto_ptr<int> p(new int(42));
std::auto_ptr<int> p = new int(42); //ERROR
p = std::auto_ptr<int>(new int(42));
p = new int(42); //ERROR
三、数值极限
整数定义于<climits> 和 <limits.h> ,浮点常数定义于 <cfloat> 和<float.h>
有两个特点:一、提供了更好的型别安全性;二、程序员可借此写出一些templates以核定这些极值。
C++标准库规定了各种类型必须保证的最小精度。以及最大精度(好像是错的,不太确定)
使用template : class numeric_limits<class T> ,在<limits>头文件中。
ex:
#include <iostream>
#include <limits>
int main(void)
{
std::cout << numeric_limits<int>::max() << std::endl;
std::cout << numeric_limits<short>::max() << std::endl;
std::cout << numeric_limits<long>::max() << std::endl;
std::cout << numeric_limits<char>::is_signed << std::endl;
std::cout << numeric_limits<std::string>::is_specialized << std::endl;
}
四、辅助函数
定义于<algorithm> 内含三个辅助函数,一个用来比较两值中挑选较大者,另一个用来
挑选较小者,第三个用来交换两值。
template<class T>
max(const T&, const T&);
template<class T>
min(const T&, const T&);
template<class T>
swap(T&, T&);
定义于<utility> 的四个比较操作符 “ !=, >, <=, >=”
<cstddef>中的定义项
NULL 指针值,用来表示“未定义”或无值
size_t 一种无正负号的型别,用来表示大小
ptrdiff_t 一种带正负号的类型, 用来表示指针之间的距离
offsetof 表示一个成员在struct 或union 中的偏移位置。
有点类似java的map,但我更想说这不是xml中的key和value吗!!!
pair 定义于 <utility>头文件,可以使用如: std::pair<int, float> p 或者
std::make_pair(int ,float) 的方法来使用pair. 这里有个例子来使用make_pair:
ex:
void f(std::pair<int, const char*>);
void g(std::pair<const int, std::string>);
...
void foo {
f(std::make_pair(42, "Hello"));
g(std::make_pair(24, "Hello"));
//with type conversions
}
其中可以看出make_pair() 使得“将两个值当做一个pair参数来传递的动作更容易记住。”
二、智能指针(auto_ptr)
auto_ptr 为了在程序中new 的对象资源,在程序结束时或需要释放资源时,没有释放(就是没有delete )。auto_ptr规定它是“它所指向的对象”的拥有者,所以当auto_ptr被摧毁时,该对象也将遭到摧毁。它要求一个对象只能拥有一个拥有者,严禁一女二夫。
可以看到auto_ptr 在解决了资源释放问题后,又引入了一个新的问题:“绝对不能以同一个对象为初值,将两个auto_ptr初始化”,这个问题多发生于该auto_ptr赋值。为了解决这个问题,令auto_ptr的copy构造函数和assignment函数将对象拥有权交出去。
如: //initialize an auto_ptr with a new object
std::auto_ptr<classA> ptr1(new classA );
//copy the auto_ptr
std::auto_ptr<classA> ptr2(ptr1);
现在,ptr1就没有那个new出来的对象的拥有权,而在ptr2上。
注:只有auto_ptr 可以拿来当做另一个auto_ptr的初值,普通指针是不行的。
使用地方:使得auto_ptr作为成员之一,在class 中使用,就可以避免遗失资源。
错误运用:
1、auto_ptr 之间不能共享拥有权。
2、并不存在针对array而设计的auto_ptr。
3、auto_ptr 绝非一个四海通用的智能指针。
4、auto_ptr 不满足STL容器对其元素的要求。
ex:
std::auto_ptr<int> p(new int(42));
std::auto_ptr<int> p = new int(42); //ERROR
p = std::auto_ptr<int>(new int(42));
p = new int(42); //ERROR
三、数值极限
整数定义于<climits> 和 <limits.h> ,浮点常数定义于 <cfloat> 和<float.h>
有两个特点:一、提供了更好的型别安全性;二、程序员可借此写出一些templates以核定这些极值。
C++标准库规定了各种类型必须保证的最小精度。以及最大精度(好像是错的,不太确定)
使用template : class numeric_limits<class T> ,在<limits>头文件中。
ex:
#include <iostream>
#include <limits>
int main(void)
{
std::cout << numeric_limits<int>::max() << std::endl;
std::cout << numeric_limits<short>::max() << std::endl;
std::cout << numeric_limits<long>::max() << std::endl;
std::cout << numeric_limits<char>::is_signed << std::endl;
std::cout << numeric_limits<std::string>::is_specialized << std::endl;
}
四、辅助函数
定义于<algorithm> 内含三个辅助函数,一个用来比较两值中挑选较大者,另一个用来
挑选较小者,第三个用来交换两值。
template<class T>
max(const T&, const T&);
template<class T>
min(const T&, const T&);
template<class T>
swap(T&, T&);
定义于<utility> 的四个比较操作符 “ !=, >, <=, >=”
<cstddef>中的定义项
NULL 指针值,用来表示“未定义”或无值
size_t 一种无正负号的型别,用来表示大小
ptrdiff_t 一种带正负号的类型, 用来表示指针之间的距离
offsetof 表示一个成员在struct 或union 中的偏移位置。
相关文章推荐
- 上海西门子培训-第二天(周一)
- 周一去神州数码面试
- ORACLE存储过程中计算某天所在周的周一和周日的方法
- 股市早8点(10月15日周一)
- C++标准库的一个有趣的小bug
- 每日一句听说学英语(周一)
- 周一好困哦!!!
- 周一遭遇战,虚拟主机遭遇arp攻击,个人空间遭遇盗链
- C++标准库中简单智能指针auto_ptr的模仿实现源码
- 《C++标准库》STL笔记3
- 090817周一
- C++标准库简介
- 从周一到周五,每天都有不一样的女主角陪您CODING!还等什么?
- 2010年8月23日周一_FeatureLayer_6.13
- C++标准库 std::list 与 std::vector性能对比
- IE9发布会,有想去的联系我,有赠票(3月21日,周一)
- C++标准库string类型学习笔记
- C++标准库学习
- 获取本周周一日期
- 又是周一