C++ //练习 19.2 默认情况下,allocator类使用operator new获取存储空间,然后使用operator delete释放它。利用上一题中的两个函数重新编译并运行你的

C++ Primer(第5版) 练习 19.2

练习 19.2 默认情况下,allocator类使用operator new获取存储空间,然后使用operator delete释放它。利用上一题中的两个函数重新编译并运行你的StrVec程序(参见13.5节,第465页)。

环境:Linux Ubuntu(云服务器)
工具:vim

 

代码块
#include<iostream>
#include<memory>
#include<utility>
using namespace std;

void *operator new(site_t size){
	if(void *mem = malloc(size)){
		return mem;
	}
	else{
		throw bad_alloc();
	}
}

void operator delete(void *mem) noexcept { free(mem); }

class StrVec{
    public:
    StrVec(): elements(nullptr), first_free(nullptr), cap(nullptr) {}
    StrVec(const StrVec &);
    StrVec &operator= (const StrVec &);
    ~StrVec();
    void push_back(const string &);
    size_t size() const { return first_free - elements; } 
    size_t capacity() const { return cap - elements; }
    string *begin() const { return elements; }
    string *end() const { return first_free; }
    void reserve(size_t s);
    void resize(size_t s);

    private:
    Static allocator<string> alloc;
    void chk_n_alloc(){ if (size() == capacity()) reallocate(); }
    pair<string*, string*>alloc_n_copy(const string*, const string*);
    void free();
    void reallocate();
    string *elements;
    string *first_free;
    string *cap;
};

void StrVec::push_back(const string &s){
    chk_n_alloc();
    alloc.construct(first_free++, s);
}

pair<string*, string*>StrVec::alloc_n_copy(const string *b, const string *e){
    auto data = alloc.allocate(e - b);
    return {data, uninitialized_copy(b, e, data)};
}

void StrVec:free(){
    if(elements){
        for(auto p = first_free; p != elements; ){
            alloc.destroy(--p);

        }
        alloc.deallocate(elements, cap - elements);
    }
}

StrVec::StrVec(const StrVec &s){
    auto newdata = alloc_n_copy(s.begin(), s.end());
    elements = newdata.first;
    first_free = cap = newdata.second;
}

StrVec::~StrVec() { free(); }

StrVec &StrVec::operator= (const StrVec &rhs){
    auto data = alloc_n_copy(rhs.begin(), rhs.end());
    free();
    elements = data.first;
    first_free = cap = data.second;
    return *this;
}

void StrVec::reallocate(){
    auto newcapacity = size() ? 2 * size() : 1;
    auto newdata = alloc.allocate(newcapacity);
    auto dest = newdata;
    auto elem = elements;
    for(size_t i = 0; i != size(); ++i){
        alloc.construct(dest++, move(*elem++));
    }
    free();
    elements = newdata;
    first_free = dest;
    cap = elements + newcapacity;
}

void StrVec::reserve(size_t s){
	if(s <= size()){
	 	return;
	}
	auto newElem = alloc.allocate(s);
	auto dest = newElem;
	auto elem = elements;
	for(size_t i = 0; i != size(); ++i){
		alloc.construct(dest++, move(*elem++));
	}
	free();
	elements = newElem;
	cap = newElem + s;
	first_free = dest;
}

void StrVec::resize(size_t s){
	if(s > capacity()){
		return ;
	}
	if(s < size()){
		auto newFisrt = first_free;
		for(size_t i = 0; i != size() - s; ++i){
			alloc.destroy(--newFirst);
		}
		fisrt_free = newFirst;
		return ;
	}
	else if(s == size()){
		return ;
	}
	else{
		auto newFirst = first_free;
		for(size_t i = 0; i != s - size(); ++i){
			alloc.construct(newFirst++, "");
		}
		first_free = newFirst;
		return ;
	}
}
  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Navigator_Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值