C++ //练习 13.40 为你的StrVec类添加一个构造函数,它接受一个Initializer_list<string>参数。

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

练习 13.40 为你的StrVec类添加一个构造函数,它接受一个Initializer_list参数。

环境:Linux Ubuntu(云服务器)
工具:vim
代码块
cpp 复制代码
/*************************************************************************
	> File Name: ex13.39.cpp
	> Author: 
	> Mail: 
	> Created Time: Fri 26 Apr 2024 08:38:31 AM CST
 ************************************************************************/

#include<iostream>
#include<memory>
#include<utility>
#include<initializer_list>
using namespace std;

class StrVec{
    public:
    StrVec(): elements(nullptr), first_free(nullptr), cap(nullptr) {}
    StrVec(const StrVec &);
    StrVec(initializer_list<string>li);
    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 ;
	}
}
相关推荐
ID_180079054735 小时前
京东获取整站实时商品详情数据|商品标题|数据分析提取教程
java·开发语言
ajassi20005 小时前
开源 C++ QT QML 开发(十七)进程--LocalSocket
c++·qt·开源
StarPrayers.6 小时前
旅行商问题(TSP)(2)(heuristics.py)(TSP 的两种贪心启发式算法实现)
前端·人工智能·python·算法·pycharm·启发式算法
微露清风6 小时前
系统性学习C++-第五讲-内存管理
java·c++·学习
qiuiuiu4136 小时前
正点原子RK3568学习日志-编译第一个驱动程序helloworld
linux·c语言·开发语言·单片机
爱吃橘的橘猫6 小时前
嵌入式系统与嵌入式 C 语言(2)
c语言·算法·嵌入式
235166 小时前
【LeetCode】146. LRU 缓存
java·后端·算法·leetcode·链表·缓存·职场和发展
molong9316 小时前
Kotlin 内联函数、高阶函数、扩展函数
android·开发语言·kotlin
盼哥PyAI实验室6 小时前
踏上编程征程,与 Python 共舞
开发语言·python
阿无,7 小时前
Java设计模式之工厂模式
java·开发语言·设计模式