关于STL模板类自定义内存分配器

namespace stl {
    template <typename _Ty>
    class allocator {
    public:
        static_assert(sizeof(_Ty) > 0, "The C++ Standard forbids containers of const elements "
            "because allocator<const T> is ill-formed.");

        typedef _Ty                             value_type;
        typedef _Ty*                            pointer;
        typedef const _Ty*                      const_pointer;
        typedef _Ty&                            reference;
        typedef const _Ty&                      const_reference;
        typedef std::size_t                     size_type;
        typedef std::ptrdiff_t                  difference_type;

        template<typename _Tp1>
        struct rebind {
            typedef allocator<_Tp1>             other;
        };

    public:
        template<typename _Tp1>
        inline constexpr allocator(allocator<_Tp1> const&) noexcept {}
        inline constexpr allocator() noexcept = default;
        inline constexpr allocator(allocator const&) noexcept = default;
        inline ~allocator() noexcept = default;

    public:
        inline constexpr pointer                address(reference r) noexcept {
            return (pointer)&reinterpret_cast<const char&>(r);
        }
        inline constexpr const_pointer          address(const_reference r) noexcept {
            return (const_pointer)&reinterpret_cast<const char&>(r);
        }

    public:
        inline pointer                          allocate(size_type n, const void* = 0) noexcept {
            return (_Ty*)Malloc(n * sizeof(_Ty));
        }
        inline void                             deallocate(pointer p, size_type n) noexcept {
            Mfree(p);
        }

    public:
        inline constexpr size_type              max_size() const noexcept {
            return static_cast<size_type>(-1) / sizeof(_Ty); // std::numeric_limits<size_type>::max();
        }

    public:
        template <typename... Args>
        inline void                             construct(pointer p, Args&&... args) noexcept {
            new (p) _Ty(std::forward<Args>(args)...);
        }
        inline void                             destroy(pointer p) noexcept {
            p->~_Ty();
        }

    public:
        inline bool                             operator==(allocator const& right) noexcept {
            const allocator* left = this;
            return left == (allocator*)&reinterpret_cast<const char&>(right);
        }
        inline bool                             operator!=(allocator const& right) noexcept {
            const allocator* left = this;
            return left != (allocator*)&reinterpret_cast<const char&>(right);
        }
    };

    template<typename _Ty, typename _Alloc = stl::allocator<_Ty> >
    using basic_string = std::basic_string<_Ty, std::char_traits<_Ty>, _Alloc >;

    using string = stl::basic_string<char>;

    using wstring = stl::basic_string<wchar_t>; // GCC -finput-charset

    using stringstream = std::basic_stringstream<char, std::char_traits<char>, stl::allocator<char> >;

    template <typename _Ty>
    using vector = std::vector<_Ty, stl::allocator<_Ty> >;

    template <typename _Ty>
    using list = std::list<_Ty, stl::allocator<_Ty> >;

    template <typename _Ty>
    using set = std::set<_Ty, stl::allocator<_Ty> >;

    template <typename _Kty, typename _Ty, typename _Pr = std::less<_Kty> >
    using map = std::map<_Kty, _Ty, _Pr, stl::allocator<std::pair<const _Kty, _Ty> > >;

    template <class _Kty, class _Hasher = std::hash<_Kty>, class _Keyeq = std::equal_to<_Kty> >
    using unordered_set = std::unordered_set<_Kty, _Hasher, _Keyeq, stl::allocator<_Kty> >;

    template <class _Kty, class _Ty, class _Hasher = std::hash<_Kty>, class _Keyeq = std::equal_to<_Kty> >
    using unordered_map = std::unordered_map<_Kty, _Ty, _Hasher, _Keyeq, stl::allocator<std::pair<const _Kty, _Ty> > >;
}

#ifndef _WIN32
namespace std {
    template<>
    struct hash<stl::string> : public std::__hash_base<size_t, stl::string> {
        inline size_t operator()(const stl::string& s) const noexcept {
            const char* p = s.data();
            return GetHashCode(p, s.size());
        }
    };

    template<>
    struct hash<stl::wstring> : public std::__hash_base<size_t, stl::wstring> {
        inline size_t operator()(const stl::wstring& s) const noexcept {
            const char* p = (char*)s.data();
            return GetHashCode(p, s.size() * sizeof(wchar_t));
        }
    };
}
#endif

inline std::string                                                  stdstr(const stl::string& v) noexcept {
    return std::string(v.data(), v.size());
}

inline stl::string                                                  stlstr(const std::string& v) noexcept {
    return stl::string(v.data(), v.size());
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值