当前位置: 代码迷 >> 综合 >> C++ STL allocator.h
  详细解决方案

C++ STL allocator.h

热度:4   发布时间:2024-02-24 00:00:46.0

全部代码

#pragma once#include "alloc.h"namespace tinySTL {template <typename T, typename Alloc>class allocator {public:using value_type         = T;using pointer            = T*;using const_pointer      = const T*;using reference          = T&;using const_reference    = const T&;using size_type          = size_t;using difference_type    = ptrdiff_t;public:static pointer allocate();static pointer allocate(size_type n);	static void deallocate(pointer ptr);static void deallocate(pointer ptr, size_type n);};template <typename T, typename Alloc>T* allocator<T, Alloc>::allocate() {return static_cast<T*>(Alloc::allocate(sizeof(T)));}template<typename T, typename Alloc>T* allocator<T, Alloc>::allocate(size_type n) {return static_cast<T*>(Alloc::allocate(sizeof(T) * n));}template<typename T, typename Alloc>void allocator<T, Alloc>::deallocate(pointer ptr) {if (!ptr) return;Alloc::deallocate(ptr, sizeof(T));}template<typename T, typename Alloc>void allocator<T, Alloc>::deallocate(pointer ptr, size_type n) {if (!ptr) return;Alloc::deallocate(ptr, sizeof(T) * n);}}

 

  相关解决方案