一,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 中的偏移位置。
c++标准库一周一记(1)
原创
©著作权归作者所有:来自51CTO博客作者kuai2361425的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:GTest使用个人总结
下一篇:我的友情链接
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
【每周一记-003】~~~
记录一下最近的状态~~
数据 技术方面 游戏开发 -
【每周一记-001】~~~
每周小总结~~~
错误码 .net 微信 -
【每周一记-002】
一周小结,iOS 萌新的XCode 使用体验~~~
xcode android 内存泄漏 -
【每周一记】域名备案终于过啦~~
耗时一个月,网站 https://glumes.com 又可以访问啦~~
服务器 Nginx Code