C++ //练习 14.23 为你的StrVec类定义一个initializer_list赋值运算符。

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

练习 14.23 为你的StrVec类定义一个initializer_list赋值运算符。

环境:Linux Ubuntu(云服务器)
工具:vim
代码块
cpp 复制代码
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);

	StrVec& operator=(std::initializer_list<std::string> li);

    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;
};

StrVec& StrVec::operator=(std::initializer_list<std::string> li){
	std::pair<std::string*, std::string*> data = alloc_n_copy(li,begin(), li.end());
	elements = data.first;
	first_free = cap = data.second;
	return *this;
}
相关推荐
灰子学技术18 小时前
go response.Body.close()导致连接异常处理
开发语言·后端·golang
那个村的李富贵19 小时前
CANN加速下的AIGC“即时翻译”:AI语音克隆与实时变声实战
人工智能·算法·aigc·cann
二十雨辰19 小时前
[python]-AI大模型
开发语言·人工智能·python
power 雀儿19 小时前
Scaled Dot-Product Attention 分数计算 C++
算法
Yvonne爱编码19 小时前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python
Re.不晚19 小时前
JAVA进阶之路——无奖问答挑战1
java·开发语言
你这个代码我看不懂19 小时前
@ConditionalOnProperty不直接使用松绑定规则
java·开发语言
pas13619 小时前
41-parse的实现原理&有限状态机
开发语言·前端·javascript
琹箐19 小时前
最大堆和最小堆 实现思路
java·开发语言·算法
renhongxia120 小时前
如何基于知识图谱进行故障原因、事故原因推理,需要用到哪些算法
人工智能·深度学习·算法·机器学习·自然语言处理·transformer·知识图谱