- 前言
- 一、allocator是what?
- 1参考一下博主文章
- 2源码分析(importance analyze)
- 二、exceptdef
- 三、Iterator
- 四、algobase
- 五、memory
- 六、function
前言
本周继续重写啦allocate(分配器), function(仿函数),iterator(迭代器),uninitalized(初始化),algobase(stl基本算法),memroy(内存管理),exceptdef(异常头文件)
本人·观点 English与math 是编程关键 大家一起进步
提示:以下是本篇文章正文内容,下面案例可供参考
一、allocator是what? 1参考一下博主文章https://blog.csdn.net/Randyhe_/article/details/79671955
简要总结
1.allocator类将内存分配回收和对象构造析构分离开来,可以让我们先分配内存再按需构造。
2.allocator类分配的内存是未构造的,为了使用已经分配好的内存,我们必须使用construct构造对象。如果使用未构造的内存,其行为是未定义的。
3.只能对真正构造了的对象进行destroy操作,用户必须保证在调用deallocate函数回收内存前对这块内存上的每个元素调用destroy函数。
T* allocator二、exceptdef::allocate()//这个函数 用来申请space { //operator new 调用底层:malloc return static_cast (::operator new(sizeof(T))); //::全局 } T* allocator ::allocate(size_type n)//size_t->size_type() //size_type是unsigned类型,表示容器中元素长度或者下标,vector ::size_type i = 0; { if (n == 0) return nullptr; return static_cast (::operator new(n * sizeof(T))); } void allocator ::deallocate(T* ptr) { if (ptr == nullptr) return; ::operator delete(ptr); } template void allocator ::deallocate(T* ptr, size_type ) { if (ptr == nullptr) return; ::operator delete(ptr); }
exceptdef是简单的异常头文件 全为宏函数
ifndef _MYTINGSTL_EXCEPTDF_H #define _MYTINGSTL_EXCEPTDF_H #include三、Iterator#include namespace mystl { #define MYSTL_DBBUG(expr) assert(expr)//assert的作用是现计算表达式 expression , //如果其值为假(即为0),那么它先向stderr打印一条出错信息, //然后通过调用 abort 来终止程序运行: #define THROW_LENGTH_ERROR_IF(expr,what) if((expr))throw std::length_error(what) #define THROW_OUT_OF_RANGE_IF(expr, what) if ((expr)) throw std::out_of_range(what) #define THROW_RUNTIME_ERROR_IF(expr, what) if ((expr)) throw std::runtime_error(what) } #endif // !1
{
// 五种迭代器类型 参考https://blog.csdn.net/qq_43152052/article/details/97121048
struct input_iterator_tag {};
struct output_iterator_tag {};
struct forward_iterator_tag : public input_iterator_tag {};
struct bidirectional_iterator_tag : public forward_iterator_tag {};
struct random_access_iterator_tag : public bidirectional_iterator_tag {};
// iterator 模板
template
struct iterator
{
typedef Category iterator_category;//种类
typedef T value_type;
typedef Pointer pointer;
typedef Reference reference;
typedef Distance difference_type;
};
四、algobase
基本函数:
max
min
copy
templateOutputIter unchecked_copy_cat(InputIter first, InputIter last, OutputIter result, mystl::input_iterator_tag) { for (; first != last; ++first, ++result) { *result = *first; } return result; } // ramdom_access_iterator_tag 版本 template OutputIter unchecked_copy_cat(RandomIter first, RandomIter last, OutputIter result, mystl::random_access_iterator_tag) { for (auto n = last - first; n > 0; --n, ++first, ++result) { *result = *first; } return result; } template OutputIter unchecked_copy(InputIter first, InputIter last, OutputIter result) { return unchecked_copy_cat(first, last, result, iterator_category(first)); } // 为 trivially_copy_assignable 类型提供特化版本 template typename std::enable_if< std::is_same ::type, Up>::value&& std::is_trivially_copy_assignable ::value, Up*>::type unchecked_copy(Tp* first, Tp* last, Up* result) { const auto n = static_cast (last - first); if (n != 0) std::memmove(result, first, n * sizeof(Up)); return result + n; } template OutputIter copy(InputIter first, InputIter last, OutputIter result) { return unchecked_copy(first, last, result); }
copy_backward
copy_if
move
move_backward//backward -向后看 bidrectioniterator
//
总结一点就是
随机迭代器与输入迭代器不一样
但是会有一个函数重载所有情况
equal
fill_n
fill
lexicographical_compare
//
// lexicographical_compare
// 以字典序排列对两个序列进行比较,当在某个位置发现第一组不相等元素时,有下列几种情况:
// (1)如果第一序列的元素较小,返回 true ,否则返回 false
// (2)如果到达 last1 而尚未到达 last2 返回 true
// (3)如果到达 last2 而尚未到达 last1 返回 false
// (4)如果同时到达 last1 和 last2 返回 false
//
mismatch
////
// mismatch
// 平行比较两个序列,找到第一处失配的元素,返回一对迭代器,分别指向两个序列中失配的元素
/
// 这个头文件负责更高级的动态内存管理
// 包含一些基本函数、空间配置器、未初始化的储存空间管理,以及一个模板类 auto_ptr
//获取对象地址 template六、functionconstexpr Tp* address_of(Tp& value)noexcept { return &value; } template pair get_buffer_helper(ptrdiff_t len, T*) { if(len>static_cast (INT_MAX/sizrof(T))); len=INT_MAX/sizeof(T); while (len > 0) { T* tmp=static_cast> (malloc(static_cast (len)*sizeof(T))); if (tmp) { return pair (tmp, len); } len /= 2;//申请失败 } return pair (nullptr, 0); }
这一部分主要是unary以及binary
binary//不懂的话就联想 string 以及 complex对象
struct plus :public binary_function{ T opretor()(const T& x, const T& y)const {return x + y; } }; template struct minus :binary_function { T opreator()(const T& x, const T& y)const {return x - y; } }; template struct multiolies :public binary_function { T operator()(const T& x, const T& y)const { return x * y; } }; template struct div :public binary_function { T operator()(const T& x, const T& y)const { return x / y; } }; template struct modulus :public binary_function { T operator()(const T& x, const T& y)const { return x % y; } }; //函数对象的否定 template struct negate :public unary_function { T operator()(const T& x)const { return -x; } }; template T identity_element(plus ) { return T(0); }



