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. [原博客] POI系列(2)

    正规.严谨.精妙. -POI bzoj 1098 : [POI2007]办公楼biu 如果把互相有手机号的建边得到一个无向图,那么这个图的补图的连通分量个数就是答案了.因为互相没手机号的必然在同一个连 ...

  2. VisualSvn Server安装和使用

    原文地址:http://www.cnblogs.com/jiahuafu/archive/2012/12/22/2828955.html VisualSvn Server介绍 1 .VisualSvn ...

  3. git svn cygwin_exception

    git for windows 2.5 版本发布了,更新之后,使用git svn的时候,就出错 git svn rebase Or git svn dcommit After a update git ...

  4. MySQL show status详解

    http://www.sandzhang.com/blog/2010/04/07/mysql-show-status-explained-detail/ 要查看MySQL运行状态,要优化MySQL运行 ...

  5. xmanager远程登录

  6. bzoj2333

    好题,先离线把连通块变成连续的区间每次连通块合并就相当于两个区间合并这样就轻易的用线段树解决了 type node=record wh:string[]; x,y:longint; end; ..*] ...

  7. bzoj1899

    显然如果只有一个窗口,是一道贪心的题目,直接让吃饭慢的排在前面即可 两个窗口的话,我们还是根据这个原则 先对吃饭时间降序排序,然后这是一个dp 假如设当前处理到第i个人,当在窗口1的打饭时间确定了,窗 ...

  8. Beta Round #9 (酱油杯noi考后欢乐赛)PLQ的寻宝

    题目:http://www.contesthunter.org/contest/Beta%20Round%20%EF%BC%839%20%28%E9%85%B1%E6%B2%B9%E6%9D%AFno ...

  9. Unity3D常见技术点(持续更新)

    一:获取对象, 添加对象等 1:使用prefab生成对象 GameObject ballObj = GameObject.Instantiate(Resources.Load(, Quaternion ...

  10. android学习——必学基础组件

    android基础组件是一个Android的开发人员必须要了解,且深刻理解的东西: 1.应用程序基础 2.应用程序组件 2.1.活动(Activities) 2.2.服务(Services) 2.3. ...