/* StrVec.h */

#ifndef _STRVEC_H_
#define _STRVEC_H_ #include <memory>
#include <string>
#include <vector>
#include <utility> class StrVec { public: StrVec() : first(nullptr), last_end(nullptr), cap(nullptr){}
StrVec(const StrVec&);
StrVec& operator = (const StrVec&);
~StrVec (); void push_back(std::string&); size_t size(){ return (last_end - first); }
size_t capacity(){ return (cap - first);} std::string* begin() const { return first; }
std::string* end() const { return last_end; } private: static std::allocator<std::string> alloc; // 分配元素 std::string* first; // first element pointer(begin())
std::string* last_end; // last_end element pointer(end())
std::string* cap; // capacity pointer std::pair<std::string*, std::string*> alloc_n_copy (const std::string*, const std::string*); void chk_n_alloc (){
if (size() == capacity())
reallocate();
} void free ();
void reallocate (); }; #endif // _STRVEC_H_
#include "StrVec.h"

StrVec::StrVec(const StrVec& s){

	auto newData =	alloc_n_copy(s.begin(), s.end());

	first		 =	newData.first;
last_end = cap = newData.second; } StrVec& StrVec::operator=(const StrVec& s){ auto data = alloc_n_copy(s.begin(), s.end());
free();
first = data.first;
last_end = cap = data.second;
return *this;
} StrVec::~StrVec(){
free();
} void StrVec::push_back(std::string& str){ chk_n_alloc();
alloc.construct(last_end++, str);
} std::pair<std::string*, std::string*> StrVec::alloc_n_copy(const std::string* begin, const std::string* end){ auto data = alloc.allocate(end - begin);
return {data, uninitialized_copy(begin, end, data)};
} void StrVec::free(){ if (nullptr != first){ for (auto p = last_end; p >= first;)
alloc.destroy(--p);
alloc.deallocate(first, cap - first);
} } void StrVec::reallocate(){ size_t newSize = size() * 2; auto newdata = alloc.allocate(newSize); auto dest = newdata;
auto fst = first; // move the old elements
for (size_t i = 0; i != size(); ++i)
alloc.construct(dest++, std::move(*fst++)); free();
first = newdata;
last_end = dest;
cap = first + newSize;
}

当使用allcator重新分配内存时,我们应该移动原来的数据而不是拷贝,如果有大量的数据,拷贝会非常浪费时间和空间资源,因此我们用到了标准库函数std::move,它定义在有文件 <utility>中。

C++ Primer : 第十三章 : 动态内存管理类的更多相关文章

  1. [C++ Primer] : 第12章: 动态内存

    动态内存与只能指针 静态内存用来保存局部static对象, 类static数据成员以及定义在任何函数之外的变量. 栈内存用来保存定义在函数内的非static对象. 分配在静态或栈内存中的对象由编译器自 ...

  2. 【C++ Primer 第13章】5. 动态内存管理类

    StrVec类的设计 [题目描述]:我们将实现标准库vector类的一个简化版本,我们所做的一个简化是不使用模板,我们类只用于string,因此,它被命名为StrVec. #include<io ...

  3. C++ Primer : 第十二章 : 动态内存之shared_ptr类实例:StrBlob类

    StrBlob是一个管理string的类,借助标准库容器vector,以及动态内存管理类shared_ptr,我们将vector保存在动态内存里,这样就能在多个对象之间共享内存. 定义StrBlob类 ...

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

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

  5. C++ Primer : 第十三章 : 拷贝控制之对象移动

    右值引用 所谓的右值引用就是必须将引用绑定到右值的引用,我们通过&&来绑定到右值而不是&, 右值引用只能绑定到即将销毁的对象.右值引用也是引用,因此右值引用也只不过是对象的别名 ...

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

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

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

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

  8. uCGUI动态内存管理

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

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

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

随机推荐

  1. 逻辑操作符---Lua: and,or,not 对比 C++:&&,||,!

    lua中有三个逻辑操作符:and,or,not(逻辑与,逻辑或,逻辑非),同样c++也有类似的三个逻辑操作符:&&,||,!(逻辑与,逻辑或,逻辑非).他们的运算对象就是真和假.lua ...

  2. kvm -- Kernel-based Virtual Machine

    1.虚拟机类型: 类型1 硬件上直接安装hp  类型2 硬件上安装HOST 上面跑VMM 2.kvm概要 kvm 不算类型1也不算类型二.两种特性都有,他是linux的一个内核模块,内核中本身没有hv ...

  3. Redis - pipelining(管道)

    客户端向服务器发送一个查询请求,并监听 socket 返回,等待服务器响应.通常是阻塞模式,在收到服务器响应之前是挂起的,不能继续发送请求. 可以使用管道来改善这种情况.在使用管道的情况下,客户端可以 ...

  4. 启动tomcat后struts框架报异常严重: Exception starting filter struts2 Unable to load configuration.

    启动tomcat后struts框架报异常严重: Exception starting filter struts2 Unable to load configuration. 出现此异常是因为,str ...

  5. SQL Server如何删除多余tempDB文件

    某时,创建了多个tempDB文件,已经超过了服务器核心数,现象删除tempDB文件,使其保持与CPU核心数相同.但是在删除的时候,发现无法删除,报出错误:无法删除文件“tempdev3”,因为它不能为 ...

  6. 23.APR/Native

    Apache Portable Runtime (APR) based Native library for Tomcat Table of Contents Introduction Install ...

  7. 支持向量机(SVM)算法

  8. cmd 打开gitshell

    C:\Users\Username\AppData\Local\GitHub\GitHub.appref-ms --open-shell

  9. Appcan跨域交互

    案例1,sina微博登录,没有插件,因此采用web方式,我首先打开https://api.weibo.com/oauth2/authorize--,然后我想增加 一个取消按钮: 1 首先打开sina ...

  10. 简单使用SQLite 的增删改查

    1.插入 第一种方式 INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Paul', 32, 'California', 200 ...