StrVec类的设计

【题目描述】:我们将实现标准库vector类的一个简化版本,我们所做的一个简化是不使用模板,我们类只用于string,因此,它被命名为StrVec。

 #include<iostream>
#include<string>
#include<memory>
using namespace std; class StrVec {
public:
StrVec(): elements(nullptr), first_free(nullptr), cap(nullptr) {}
StrVec(const StrVec &);
StrVec& operator=(const StrVec&);
~StrVec() { free(); }; StrVec(StrVec &&s) noexcept;
StrVec& opearator=(StrVec &&rhs) noexcept; 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; } 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(const StrVec &s)
{
auto newdata = alloc_n_copy(s.begin(), s.end());
elements = newdata.first;
first_free = cap = newdata.second;
} StrVec& StrVec::operator=(const StrVec &rhs)
{
auto data = alloc_n_copy(rhs.begin(), rhs.end());
free();
elements = data.first;
first_free = cap = newdata.second;
return *this;
} void StrVec::free()
{
if (elements)
{
for (auto p = first_free; p != elements; )
alloc.destroy(--p);
alloc.deallocate(elements, cap - elements);
}
} 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::push_back(const string& s)
{
chk_n_alloc();
alloc.construct(first_free++, s);
} void StrVec::reallocate()
{
auto newcapacity = size() ? * size() : ;
auto newdata = alloc.allocate(newcapacity);
auto dest = newdata;
auto elem = elements; //原对象的elements指针
for (size_t i = ; i != size(); ++i)
alloc.construct(dest++, std::move(*elem++));
free();
elements = newdata;
first_free = dest;
cap = elements + newcapacity;
} StrVec::StrVec(StrVec &&s) noexcept
: elements(s.elements), first_free(s.first_free), cap(s.cap)
{
s.elements = s.first_free = cap = nulllptr;
} StrVec& StrVec::StrVec(StrVec &&s) noexcept
{
if (this = &s)
{
free();
elemens = rhs.elements;
first_free = .frhsirst_free;
cap = rhs.cap;
rhs.elements = srhs.first_free = rhs.cap = nullptr;
}
return *this;
}

3.5节练习

• 编写标准库string类的简化版本,命名为string。

 #include<iostream>
#include<memory>
#include<cstring>
#include<initializer_list>
using namespace std; class String {
friend String operator+(const String&, const String&);
friend String add(const String&, const String&);
friend ostream &operator<<(std::ostream&, const String&);
friend ostream &print(std::ostream&, const String&); public:
String():sz(), p(nullptr) {};
String(const char *cp): sz(strlen(cp)), p(p.allocate(sz)) { uninitialized_copy(cp, cp + sz, p)}
String(const String &s): sz(s.sz), p(a.allocate(sz)) { uninitialized_copy(s.p, s.p + sz, p)}
String(size_t n, char c): sz(n), p(a.allocate(n)) {uninitialized_fill_n(p, n, t)}
~String() noexcept { if (p) a.deallocate(p, sz); } String &operator=(const String &);
String &operator=(const char*);
String &operator=(char);
String &operator=(initializer_list<char>); const char *begin() { return p; }
const char *begin() const { return p; }
const char *end() { return p + sz; }
const char *end() const { return p + sz; }
size_t size() const { return sz; }
void swap(String &s); private:
static allocator<char> a;
size_t sz;
char *p;
}; /***********************************************************************************/
ostream &operator<<(ostream &os, const string &s)
{
return print(os, rhs);
} ostream &print(ostream &os, const string &s)
{
auto p = s.begin();
while(p != s.end())
os << *p++;
return os;
} string add(const string &lhs, const string &rhs)
{
string ret;
ret.sz = lhs.size() + rhs.size();
ret.p = a.allocate(ret.sz);
uninitialized_copy(lhs.begin(), lhs.end(),ret.p);
uninitialized_copy(rhs.begin(), rhs.end(), ret.p + lhs.sz);
return ret;
} string operator+(const string &lhs, const string &rhs)
{
return add(lhs, rhs);
}
/************************************************************************************/ String &String::operator=(const String &rhs)
{
auto newp = a.allocator(rhs.sz);
uninitlized_copy(rhs.p, rhs.p + rhs.ze, newp);
if (p)
a.deallocate(p, sz);
p = newp;
sz = rhs.sz;
return *this;
} String &String::operator=(const char *cp)
{
if(p)
a.deallocate(p, sz);
p = a.allocate(sz = strlen(cp));
uninitialzied_copy(cp, cp + sz, p);
return *this;
} String &String::operator=(char c)
{
if(p)
a.deallocate(p, sz);
p = a.allocate(sz = );
*p = c;
return *this;
} String& string::operator=(initializer_list<char> il)
{
if(p)
a.deallocate(p, sz);
p = a.llocate(il.size());
uninitialized_copy(il.begin(), il.end(), p);
return *this;
} /***************************************************************************************/ void string::swap(String &s)
{
auto tmp = p;
p = s.p;
s.p = tmp;
auto cnt = sz;
sz = s.sz;
s.sz = cnt;
} void swap(String &s1, String &s2)
{
s1.swap(s2); String make_plural(size_t ctr, const String &, const String &)
{
return (ctr != ) ? add(word, ending) : word;
}

【C++ Primer 第13章】5. 动态内存管理类的更多相关文章

  1. C++ Primer : 第十三章 : 动态内存管理类

    /* StrVec.h */ #ifndef _STRVEC_H_ #define _STRVEC_H_ #include <memory> #include <string> ...

  2. [C++ Primer] : 第13章: 拷贝控制

    拷贝, 赋值与销毁 当定义一个类时, 我们显示地或隐式地指定在此类型的对象拷贝, 移动, 赋值和销毁时做什么. 一个类通过定义5种特殊的成员函数来控制这些操作, 包括: 拷贝构造函数, 拷贝赋值运算符 ...

  3. (原创)动态内存管理练习 C++ std::vector<int> 模拟实现

    今天看了primer C++的 “动态内存管理类”章节,里面的例子是模拟实现std::vector<std::string>的功能. 照抄之后发现编译不通过,有个库函数调用错误,就参考着自 ...

  4. C++动态内存管理与源码剖析

    引言 在本篇文章中,我们主要剖析c++中的动态内存管理,包括malloc.new expression.operator new.array new和allocator内存分配方法以及对应的内存释放方 ...

  5. C++动态内存管理之shared_ptr、unique_ptr

    C++中的动态内存管理是通过new和delete两个操作符来完成的.new操作符,为对象分配内存并调用对象所属类的构造函数,返回一个指向该对象的指针.delete调用时,销毁对象,并释放对象所在的内存 ...

  6. uCGUI动态内存管理

    动态内存的堆区 /* 堆区共用体定义 */ typedef union { /* 可以以4字节来访问堆区,也可以以1个字节来访问 */ ]; /* required for proper aligne ...

  7. Keil C动态内存管理机制分析及改进(转)

    源:Keil C动态内存管理机制分析及改进 Keil C是常用的嵌入式系统编程工具,它通过init_mempool.mallloe.free等函数,提供了动态存储管理等功能.本文通过对init_mem ...

  8. FreeRTOS 动态内存管理

    以下转载自安富莱电子: http://forum.armfly.com/forum.php 本章节为大家讲解 FreeRTOS 动态内存管理,动态内存管理是 FreeRTOS 非常重要的一项功能,前面 ...

  9. C++程序设计入门 引用和动态内存管理学习

    引用: 引用就是另一个变量的别名,通过引用所做的读写操作实际上是作用于原变量上. 由于引用是绑定在一个对象上的,所以定义引用的时候必须初始化. 函数参数:引用传递 1.引用可做函数参数,但调用时只需 ...

随机推荐

  1. windows单机环境下配置tomcat集群

    场景:我们在平常联系中,需要涉及到tomcat中,但是电脑不够怎么办,肯定是在自己的电脑上模拟集群,就是装多个tomcat,这时候需要稍微配置下.如果是多个服务器,那不用配置,直接怼!!! 这里介绍的 ...

  2. Linux 命令详解(九)轻易删除OpenSSL 的后果

    警告自己,不要轻易的去删除系统的软件 1.composer自动更新出现错误 www@TinywanAliYun:~/web/go-study-line$ composer self-update Ke ...

  3. vue中$mount与el区别

    vue中的$mount和el两者在使用中没有什么区别,都是将实例化后的vue挂载到指定的DOM元素中 用法: 如果在实例化vue时指定el,则该vue将会渲染在el对应的DOM中 反之 没有指定el, ...

  4. .net 读写xml

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. luogu P4342 [IOI1998]Polygon

    IOI早期这么多dp? 题目要求断掉环上的一边,我们可以断环为链,开两倍数组 容易想到dp,设\(f_{i,j}\)为区间\([i,j]\)的最大值,然后就是个枚举断点的区间dp 不过可能会有负数出现 ...

  6. php ldap

    参考链接: http://blog.csdn.net/guoyuqi0554/article/details/11015403 http://blog.csdn.net/qk1992919/artic ...

  7. IMU 预积分推导

    给 StereoDSO 加 IMU,想直接用 OKVIS 的代码,但是有点看不懂.知乎上郑帆写的文章<四元数矩阵与 so(3) 左右雅可比>提到 OKVIS 的预积分是使用四元数,而预积分 ...

  8. linux 网卡

    查看网卡UUID:nmcli con show    或    nmcli con list 查看网卡mac地址:nmcli dev show    或    nmcli dev list 注:sho ...

  9. 【比赛游记】NOIP2018游记

    往期回顾:[比赛游记]NOIP2017游记 转眼间又过去了一年,当年还是初中生的我已经摇身一变成为了AHSOFNU的高一学生. 回顾这一年我好像也没学什么新东西,要说有用的可能就无旋Treap吧,不知 ...

  10. Python中【__all__】的用法

    Python中[__all__]的用法 转:http://python-china.org/t/725 用 __all__ 暴露接口 Python 可以在模块级别暴露接口: __all__ = [&q ...