c++ stl library 学习(5)

来源:互联网 发布:建党伟业有一天 知乎 编辑:程序博客网 时间:2024/06/06 02:28

Numeric Limits

old :#include<limits.h> or #include <climits>

standard : #include <limits>

Class numeric_limits


Auxiliary Functions:in #include<algorithm>  min 、max、 swap

namespace std {
template <class T>
inline const T& min (const T& a, const T& b) {
return b < a ? b : a;
}

template <class T>
inline const T& max (const T& a, const T& b) {
return a < b ? b : a;
}
}


namespace std {
template <class T, class Compare>
inline const T& min (const T& a, const T& b, Compare comp) {
return comp(b,a) ? b : a;
}
template <class T, class Compare>
inline const T& max (const T& a, const T& b, Compare comp) {
return comp(a,b) ? b : a;
}
}//comp为函数或者伪函数指针

must type match,参数类型要一致。。in min或max。


namespace std {
template<class T>
inline void swap(T& a, T& b) {
T tmp(a);
a = b;
b = trap;
}
}

在c++中经常要用到的c的标准库 #include <cstddef> 和#include <cstdlib>

Note thatin C, NULL often is defined as (void*)0. This is incorrect in C++ because there the type of NULL must be an integer type

c中NULL被定义为(void*)0,但c++中不是这样的,它必须是int类型的,


The exit() and abort() functions are provided to terminate a program in any function without
going back to main():
• exit() destroys all static objects, flushes all buffers, closes all I/O channels, and
terminates the program (including calling atexit() functions). If functions passed to
atexit() throw exceptions, terminate() is called.
• abort() terminates a program immediately with no clean up.

None of these functions destroys local objects because no stack unwinding occurs. To ensure
that the destructors of all local objects are called, you should use exceptions or the ordinary
return mechanism to return to and exit main(). 


原创粉丝点击