13.44 编写标准库string类的简化版本,命名String。你的类应该至少有一个默认构造函数和一个接受C风格字符串指针参数的构造函数。使用allocator为你的String类分配所需内存。

13.47 添加拷贝构造函数和拷贝赋值运算符,并添加打印语句,则每次函数执行时打印一条信息。

13.48 定义一个vector<String>并在其上多次调用push_back。运行程序,观察String被拷贝了多少次。

#include<iostream>
#include<string>
#include<memory>
#include<utility>
#include<cstring>
#include<vector>
using namespace std; class String
{
public:
String()=default;
String(char *c);
String(const String&);
String& operator=(const String&);
string* begin() const { return elements;}
string* end() const { return first_free;}
private:
static allocator<string> alloc;
string *elements;
string *first_free;
}; allocator<string> String::alloc;
String::String(char *c)
{
size_t capacity=strlen(c);
auto data=alloc.allocate(capacity);
auto dest=data;
string s;
s.copy(c,strlen(c));
alloc.construct(dest++,s);
elements=data;
first_free=dest;
} String::String(const String &s)
{
cout<<"copy construct"<<endl;
auto capacity=s.end()-s.begin();
auto data=alloc.allocate(capacity);
uninitialized_copy(s.begin(),s.end(),data);
elements=data;
first_free=data+capacity;
} String& String::operator=(const String &s)
{
cout<<"copy = construct"<<endl;
auto capacity=s.end()-s.begin();
auto data=alloc.allocate(capacity);
uninitialized_copy(s.begin(),s.end(),data);
if(elements)
{
auto begin=elements;
auto end=first_free;
while(begin!=end)
alloc.destroy(begin++);
alloc.deallocate(elements,first_free-elements);
}
elements=data;
first_free=data+capacity;
return *this;
}
int main()
{
vector<String> vec;
char ch[]="hello";
char ch1[]="world!";
cout<<vec.capacity()<<endl;
cout<<endl;
vec.push_back(String(ch));
cout<<vec.capacity()<<endl;
cout<<endl;
vec.push_back(String(ch1));
cout<<vec.capacity()<<endl;
cout<<endl;
vec.push_back(String(ch));
cout<<vec.capacity()<<endl;
cout<<endl;
vec.push_back(String(ch1));
cout<<vec.capacity()<<endl;
cout<<endl;
vec.push_back(String(ch1));
cout<<vec.capacity()<<endl;
cout<<endl;
vec.push_back(String(ch1));
cout<<vec.capacity()<<endl;
return ;
}

运行结果:

解释:http://blog.csdn.net/HEYUTAO007/article/details/6702626

自定义String类,并且实现在STL容器中添加自定义的类型的更多相关文章

  1. C++基础 (5) 第五天 重载new delete () 只能操作符 自定义string类

    1 昨日回顾 1.static 对整个类共享 可以直接用 类::方法 调用 如果是私有的 可以提供一个静态的访问静态成员的方法 2 自定义的数组类-重载操作符[] 3 重载new和delete 4 重 ...

  2. 洛谷 P1308 统计单词数【string类及其函数应用/STL】

    题目描述 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数. 现在,请你编程实现这一功能,具体要求是:给定一个单词,请你输出它在给 ...

  3. 自定义string类

    #include <iostream> #include <cstring> using namespace std; class String; class Data{ // ...

  4. 删除STL容器中的元素

    有关stl容器删除元素的问题,错误的代码如下: std::vector<struct> mFriendList; ... std::vector<struct>::iterat ...

  5. c++中STL容器中的排序

    1.c++STL中只有list自带了排序函数: (1).若list中存放的是int类型或者string类型,直接利用sort即可: list <int> list1;           ...

  6. traits编程---萃取容器中迭代器的类型等

    可以直接利用STL中定义好的traits_iterator来萃取 /*特性萃取器*/ template <class unknown_class> struct unknown_class ...

  7. C++——string类和标准模板库

    一.string类 1.构造函数 string实际上是basic_string<char>的一个typedef,同时省略了与内存管理相关的参数.size_type是一个依赖于实现的整型,是 ...

  8. C++ primer plus读书笔记——第16章 string类和标准模板库

    第16章 string类和标准模板库 1. string容易被忽略的构造函数: string(size_type n, char c)长度为n,每个字母都为c string(const string ...

  9. STL容器之一vector

    STL中最简单也是最有用的容器之一是vector<T>类模板,称为向量容器,是序列类型容器中的一种. 1.vector<T> 对象的基本用法(1)声明:vector<ty ...

随机推荐

  1. 关于在WIN32调用一些Zw系列的文件操作函数

    转自:http://blog.csdn.net/cooblily/archive/2007/10/27/1848037.aspx 都好久沒上來写文章了,都不知道做什么好,結果还是学写了一下用Nativ ...

  2. VC禁止在任务管理器中结束本进程

    转自百度空间:http://hi.baidu.com/175943462/item/657905e13b73b70b8d3ea8bb 一提到进程保护特别是在Windows下,没有最安全,只有更安全.下 ...

  3. 开始hadoop

    hadoop介绍 分布式存储系统HDFS(Hadoop Distributed File System),提供了高可靠性.高扩展性和高吞吐率的数据存储服务: 资源管理系统YARN(Yet Anothe ...

  4. maven 下载 源码和javadoc命令

    1:Maven命令下载源码和javadocs 当在IDE中使用Maven时如果想要看引用的jar包中类的源码和javadoc需要通过maven命令下载这些源码,然后再进行引入,通过mvn命令能够容易的 ...

  5. HDOJ -- 4699

    Editor Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

  6. Linux之sed详解

    转载:http://blog.chinaunix.net/u/22677/showart_1076318.html   1.简介 sed是非交互式的编辑器.它不会修改文件,除非使用shell重定向来保 ...

  7. Linux下Java环境变量设置

    我用的是oh my zsh,需要修改.zshrc,记一下免得以后忘记在哪儿改的. 如果你用的是自带的terminal那么用文本编辑器打开用户目录下的.bash_profile文件 在.bash_pro ...

  8. ARM学习笔记13——LED驱动程序设计

    首先我们要根据开发板原理图得到控制LED灯的引脚是哪个,我们现在以LED1为例,我们已经知道LED1由S5PV210的GPC1_3控制,因此我们按如下步骤进行: 第一步是配制S5PV210的GPC1_ ...

  9. Lua 中使用面向对象(续)

    上一篇文章给了一个面向对象的方案,美中不足的是没有析构函数 Destructor,那么这一次就给它加上. 既然是析构,那么就是在对象被销毁之前做该做的事情,lua 5.1 的 userdata 可以给 ...

  10. weekend110(Hadoop)的 第五天笔记

    (2015年1月24日) 课程目录 01-zookeeper1 02-zookeeper2 03-NN高可用方案的要点1 04-hadoop-HA机制的配置文件 05-hadoop分布式集群HA模式部 ...