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 ;
	}
}
相关推荐
故事不长丨6 分钟前
C#定时器与延时操作的使用
开发语言·c#·.net·线程·定时器·winform
hefaxiang9 分钟前
C语言常见概念(下)
c语言·开发语言
“αβ”15 分钟前
MySQL表的操作
linux·网络·数据库·c++·网络协议·mysql·https
potato_may18 分钟前
链式二叉树 —— 用指针构建的树形世界
c语言·数据结构·算法·链表·二叉树
欧阳天风19 分钟前
js实现鼠标横向滚动
开发语言·前端·javascript
十五年专注C++开发34 分钟前
Asio2: 一个基于 Boost.Asio 封装的高性能网络编程库
网络·c++·boost·asio·asio2
gcfer36 分钟前
CS144 中的C++知识积累
c++·右值引用·智能指针·optional容器
java修仙传44 分钟前
每日一题,力扣560. 和为 K 的子数组
算法·leetcode
yue0081 小时前
C# Directory的用法介绍
开发语言·c#
ada7_1 小时前
LeetCode(python)——148.排序链表
python·算法·leetcode·链表