zoukankan      html  css  js  c++  java
  • C++ Primer : 第十三章 : 动态内存管理类

    /* StrVec.h */
    
    #ifndef _STRVEC_H_
    #define _STRVEC_H_
    
    #include <memory>
    #include <string>
    #include <vector>
    #include <utility>
    
    class StrVec {
    
    public:
    	
    	StrVec() : first(nullptr), last_end(nullptr), cap(nullptr){}
    	StrVec(const StrVec&);
    	StrVec& operator = (const StrVec&);
    	~StrVec ();
    
    
    	void push_back(std::string&);
    
    	size_t size(){ return (last_end - first); }
    	size_t capacity(){ return (cap - first);}
    
    	std::string* begin() const { return first; }
    	std::string* end() const { return last_end; }
    
    
    private:
    
    	static std::allocator<std::string> alloc; // 分配元素
    
    	std::string* first;		 // first element pointer(begin())
    	std::string* last_end;	 // last_end element pointer(end())
    	std::string* cap;		 //	capacity pointer
    
    	std::pair<std::string*, std::string*> alloc_n_copy (const std::string*, const std::string*);
    
    	void chk_n_alloc (){
    		if (size() == capacity())
    			reallocate();
    	}
    
    	void free ();
    	void reallocate ();
    
    };
    
    #endif // _STRVEC_H_


    #include "StrVec.h"
    
    StrVec::StrVec(const StrVec& s){
    
    	auto newData =	alloc_n_copy(s.begin(), s.end());
    
    	first		 =	newData.first;
    	last_end	 =  cap = newData.second;
    
    }
    
    StrVec& StrVec::operator=(const StrVec& s){
    
    	auto data = alloc_n_copy(s.begin(), s.end());
    	free();
    	first = data.first;
    	last_end = cap = data.second;
    	return *this;
    }
    
    StrVec::~StrVec(){
    	free();
    }
    
    void StrVec::push_back(std::string& str){
    
    	chk_n_alloc();
    	alloc.construct(last_end++, str);
    }
    
    std::pair<std::string*, std::string*> StrVec::alloc_n_copy(const std::string* begin, const std::string* end){
    
    	auto data = alloc.allocate(end - begin);
    	return {data, uninitialized_copy(begin, end, data)};
    }
    
    void StrVec::free(){
    
    	if (nullptr != first){
    
    		for (auto p = last_end; p >= first;)
    			alloc.destroy(--p);
    		alloc.deallocate(first, cap - first);
    	}
    
    }
    
    void StrVec::reallocate(){
    
    	size_t newSize =  size() * 2;
    		
    	auto newdata   = alloc.allocate(newSize);
    
    	auto dest  = newdata;
    	auto fst   = first;
    
    	// move the old elements
    	for (size_t i = 0; i != size(); ++i)
    		alloc.construct(dest++, std::move(*fst++));
    
    	free();
    	first		=	newdata;
    	last_end	=	dest;
    	cap			=	first + newSize;
    }
    


    当使用allcator重新分配内存时,我们应该移动原来的数据而不是拷贝,如果有大量的数据,拷贝会非常浪费时间和空间资源,因此我们用到了标准库函数std::move,它定义在有文件 <utility>中。




  • 相关阅读:
    统计学(第六版)14单元——学习总结
    统计学(第六版)13单元——学习总结(时间序列分析总结)
    统计学(第六版)11到12单元——学习总结
    Kubernetes: 微内核的分布式操作系统
    彻底搞懂JavaScript之原型
    手把手带你玩转k8s-一键部署vue项目
    新一代缓存Caffeine,速度确实比Guava的Cache快
    理解 Es6 中的 Symbol 类型
    一天一大 leet(用两个栈实现队列)难度:简单 DAY-30
    (Java 源码阅读) 春眠不觉晓,HashMap知多少
  • 原文地址:https://www.cnblogs.com/averson/p/5096052.html
Copyright © 2011-2022 走看看