探究stl2

追求适度,才能走向成功;人在顶峰,迈步就是下坡;身在低谷,抬足既是登高;弦,绷得太紧会断;人,思虑过度会疯;水至清无鱼,人至真无友,山至高无树;适度,不是中庸,而是一种明智的生活态度。

导读:本篇文章讲解 探究stl2,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文


前言

本周继续重写啦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函数。

2源码分析(importance analyze)

 T* allocator<T>::allocate()//这个函数 用来申请space
  {    //operator new 调用底层:malloc 
        return static_cast<T*>(::operator new(sizeof(T)));
        //::全局
  }
 T* allocator<T>::allocate(size_type n)//size_t->size_type()
       //size_type是unsigned类型,表示容器中元素长度或者下标,vector<int>::size_type i = 0;
 {
  if (n == 0) return nullptr;
 return static_cast<T*>(::operator new(n * sizeof(T)));
  }
 void allocator<T>::deallocate(T* ptr)
{
if (ptr == nullptr) return;
::operator delete(ptr);
}

template <class T>
void allocator<T>::deallocate(T* ptr, size_type /*size*/)
{
  if (ptr == nullptr)
    return;
  ::operator delete(ptr);
}
    


二、exceptdef

exceptdef是简单的异常头文件 全为宏函数

ifndef _MYTINGSTL_EXCEPTDF_H
#define _MYTINGSTL_EXCEPTDF_H
#include<stdexcept>
#include<cassert>
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

三、Iterator

{

	// 五种迭代器类型 参考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 <class Category, class T, class Distance = ptrdiff_t,
		class Pointer = T*, class Reference = T&>
		struct iterator
	{
		typedef Category                             iterator_category;//种类
		typedef T                                    value_type;
		typedef Pointer                              pointer;
		typedef Reference                            reference;
		typedef Distance                             difference_type;
	};
	

四、algobase

基本函数:
max
min
copy

 template <class InputIter, class OutputIter>
    OutputIter
        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 <class RandomIter, class OutputIter>
    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 <class InputIter, class OutputIter>
    OutputIter
        unchecked_copy(InputIter first, InputIter last, OutputIter result)
    {
        return unchecked_copy_cat(first, last, result, iterator_category(first));
    }

    // 为 trivially_copy_assignable 类型提供特化版本
    template <class Tp, class Up>
    typename std::enable_if<
        std::is_same<typename std::remove_const<Tp>::type, Up>::value&&
        std::is_trivially_copy_assignable<Up>::value,
        Up*>::type
        unchecked_copy(Tp* first, Tp* last, Up* result)
    {
        const auto n = static_cast<size_t>(last - first);
        if (n != 0)
            std::memmove(result, first, n * sizeof(Up));
        return result + n;
    }

    template <class InputIter, class OutputIter>
    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
// 平行比较两个序列,找到第一处失配的元素,返回一对迭代器,分别指向两个序列中失配的元素
/

五、memory

// 这个头文件负责更高级的动态内存管理
// 包含一些基本函数、空间配置器、未初始化的储存空间管理,以及一个模板类 auto_ptr

//获取对象地址
	template<class Tp>
	constexpr Tp* address_of(Tp& value)noexcept
	{
		return &value;
	}
	template<class T>
	pair<T*, ptrdiff_t>get_buffer_helper(ptrdiff_t len, T*)
	{
		if(len>static_cast<ptrdiff_t>(INT_MAX/sizrof(T)));
			len=INT_MAX/sizeof(T);
			while (len > 0)
			{
				T* tmp=static_cast><T*>(malloc(static_cast<size_t>(len)*sizeof(T)));
				if (tmp)
				{
					return pair<T*, ptrdiff_t>(tmp, len);
				}
				len /= 2;//申请失败
			}
			return pair<T*, ptrdiff_t>(nullptr, 0);
	}

六、function

这一部分主要是unary以及binary
binary//不懂的话就联想 string 以及 complex对象

struct plus :public binary_function<T, T, T>
	{
		T opretor()(const T& x, const T& y)const
		{return x + y; }
	};
	template<class T>
	struct minus :binary_function<T, T, T>
	{
		T opreator()(const T& x, const T& y)const
		{return x - y; }
	};
	template<class T>
	struct multiolies :public binary_function<T, T, T>
	{
		T operator()(const T& x, const T& y)const
		{
			return  x * y;
		}

	};
	template<class T>
	struct div :public binary_function<T, T, T>
	{
		T operator()(const T& x, const T& y)const
		{
			return  x / y;
		}

	};
	template<class T>
	struct modulus :public binary_function<T, T, T>
	{
		T operator()(const T& x, const T&)const
		{
			return x % y;
		}

	};
	//函数对象的否定
	template<class T>
	struct negate :public unary_function<T, T>
	{
		T operator()(const T& x)const { return -x; }
	};
	template <class T>
	T identity_element(plus<T>) { return T(0); }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/129745.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!