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

本文主要是介绍C++ //练习 13.40 为你的StrVec类添加一个构造函数,它接受一个Initializer_list<string>参数。,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

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

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

 

代码块
/*************************************************************************> 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 ;}
}

这篇关于C++ //练习 13.40 为你的StrVec类添加一个构造函数,它接受一个Initializer_list<string>参数。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/938066

相关文章

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

C++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

HTTP 与 SpringBoot 参数提交与接收协议方式

《HTTP与SpringBoot参数提交与接收协议方式》HTTP参数提交方式包括URL查询、表单、JSON/XML、路径变量、头部、Cookie、GraphQL、WebSocket和SSE,依据... 目录HTTP 协议支持多种参数提交方式,主要取决于请求方法(Method)和内容类型(Content-Ty

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

python中的显式声明类型参数使用方式

《python中的显式声明类型参数使用方式》文章探讨了Python3.10+版本中类型注解的使用,指出FastAPI官方示例强调显式声明参数类型,通过|操作符替代Union/Optional,可提升代... 目录背景python函数显式声明的类型汇总基本类型集合类型Optional and Union(py

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数

C++ vector越界问题的完整解决方案

《C++vector越界问题的完整解决方案》在C++开发中,std::vector作为最常用的动态数组容器,其便捷性与性能优势使其成为处理可变长度数据的首选,然而,数组越界访问始终是威胁程序稳定性的... 目录引言一、vector越界的底层原理与危害1.1 越界访问的本质原因1.2 越界访问的实际危害二、基

redis数据结构之String详解

《redis数据结构之String详解》Redis以String为基础类型,因C字符串效率低、非二进制安全等问题,采用SDS动态字符串实现高效存储,通过RedisObject封装,支持多种编码方式(如... 目录一、为什么Redis选String作为基础类型?二、SDS底层数据结构三、RedisObject