使用alloctor模板来实现string类
虽然以前做过更复杂的各种数据结构,不过那只是在看完c++prime7章后做的,没有考虑到类的拷贝体现出来是类值还是类指针,于是写了一些半成品类,不过那些主要是练数据结构,不想再改,于是就想办法模仿了下string,以前都是使用new和delete,虽然简单,但是不够灵活,于是就刻意使用alloctor模板来管理内存,这样可以将“内存分配”与“对象构造分开”,因为对象并不是指针这类管理外部资源的对象,于是我就干脆不destory,这样使用alloctor效率优势更加有体现,练下手熟,总的来说没什么难度。
#include <iostream>
#include <string>
#include <memory>
using namespace std;
const int incerment=;
class String{
public:
friend istream& operator >> (istream &is, const String &s);
friend ostream& operator << (ostream &os, const String &s);
friend const String operator + (const String& a,const String& b);
String() :elements(nullptr), last_element(nullptr), space_end(nullptr){}
String(const char *s) :String(){
for (size_t i = ; s[i]; i++)
push_back(s[i]);
}
String(const String &s) :elements(nullptr), last_element(nullptr), space_end(nullptr){
elements = alloc.allocate(s.size() * );
last_element = uninitialized_copy(s.begin(), s.end(), elements);
space_end = elements + s.size() * ;
uninitialized_fill(last_element, space_end, );
}
String(const size_t &len) :elements(nullptr), last_element(nullptr), space_end(nullptr){
elements = alloc.allocate(len);
uninitialized_fill_n(elements, len, );
last_element = elements;
space_end = elements + len;
}
String(char *b, char *e):String(){
auto p = b;
while (p != e){
push_back(*p++);
}
}
String(size_t n, char ch):String(){
elements = alloc.allocate(n*);
uninitialized_fill_n(elements, n, ch);
last_element = elements + n;
space_end = elements + n * ;
}
~String(){
alloc.deallocate(elements, capcity());
};
void swap(String &a, String &b){
using std::swap;
swap(a.elements, b.elements);
swap(a.last_element, b.last_element);
swap(a.space_end, b.space_end);
}
void clear(){
alloc.deallocate(elements, capcity());
elements = last_element = space_end = nullptr;
}
void push_back(char ch){
if (size() >= capcity()){
auto newcap = capcity() + incerment;
auto p = alloc.allocate(newcap);
if (elements)
last_element = uninitialized_copy(elements, last_element, p);
else{
elements = last_element = p;
}
space_end = elements + newcap;
uninitialized_fill(last_element, space_end, );
}
*last_element++ = ch;
}
String& operator = (String s){
swap(*this, s);
return *this;
}
char &operator [] (const size_t i){
return elements[i];
}
const char& operator [] (const size_t i)const{
return elements[i];
}
bool operator == (const String &s)const{
if (size() != s.size())
return false;
for (size_t i = ;elements+i!=last_element;i++)if(elements[i]!=s[i]){
return false;
}
return true;
}
bool operator != (const String &s)const{
return !(equal(elements, last_element, s.begin()));
}
String& operator += (const String& s){
auto newcap = capcity() + s.capcity();
auto p = alloc.allocate(newcap);
uninitialized_copy(elements, last_element, p);
last_element = uninitialized_copy(s.begin(), s.last_element, p + size());
alloc.deallocate(elements,capcity());
elements = p;
space_end = elements + newcap;
uninitialized_fill(last_element,space_end,);
return *this;
}
size_t size()const{ return last_element-elements; }
size_t capcity()const{ return space_end - elements; }
char*begin()const{return elements;}
char *end()const{ return last_element; } private:
allocator<char> alloc;
char *elements,*last_element,*space_end;
};
istream& operator >> (istream &is,String &s){
char buf;
while (is>>buf){
s.push_back(buf);
}
return is;
}
ostream&operator << (ostream&os, const String&s){
auto p = s.begin();
while (p != s.end()){
os << *p++;
}
return os;
}
const String operator + (const String& a, const String& b){
shared_ptr<String> res=make_shared<String>(a);
*res += b;
return *res;
}
int main(){
{
String a("a"), b = "b",c;
cout << "a = " << a << " b = " << b << " c == " << c << endl;
c = b;
puts(c == b ? "Yes" : "No");
cout << "a = " << a << " b = " << b << " c == " << c << endl;
a += b;
cout << "a = " << a << " b = " << b << " c == " << c << endl;
c = "c";
puts(c == b ? "Yes" : "No");
cout << "a = " << a << " b = " << b << " c == " << c << endl;
c += "d";
cout << "a = " << a << " b = " << b << " c == " << c << endl;
b = a + c;
cout << "a = " << a << " b = " << b << " c == " << c << endl;
String d(b.begin()+,b.end()), e(,'e'),f="f"+e;
cout << "e.capcity = " << e.capcity() <<" d = "<<d<<" e = "<<e<< endl;
f += 'g';
cout << "a = " << a << " b = " << b << " c == " << c << endl;
String h = "h";
h = b + h;
cout << h[] <<" "<< h[h.size() - ];
}
_CrtDumpMemoryLeaks();
}
使用alloctor模板来实现string类的更多相关文章
- C++——string类和标准模板库
一.string类 1.构造函数 string实际上是basic_string<char>的一个typedef,同时省略了与内存管理相关的参数.size_type是一个依赖于实现的整型,是 ...
- 《C++ Primer Plus》第16章 string类和标准模板库 学习笔记
C++提供了一组功能强大的库,这些库提供了很多常见编程问题的解决方案以及简化其他问题的工具string类为将字符串作为对象来处理提供了一种方便的方法.string类提供了自动内存管理动能以及众多处理字 ...
- C++标准模板库Stand Template Library(STL)简介与STL string类
参考<21天学通C++>第15和16章节,在对宏和模板学习之后,开启对C++实现的标准模板类STL进行简介,同时介绍简单的string类.虽然前面对于vector.deque.list等进 ...
- C++ primer plus读书笔记——第16章 string类和标准模板库
第16章 string类和标准模板库 1. string容易被忽略的构造函数: string(size_type n, char c)长度为n,每个字母都为c string(const string ...
- 面向对象(类,实例变量,方法定义,方法重载,构造方法,this,string类)
面向对象 类是由属性和方法组成 类是所有创建对象的模板 实例变量有默认值 实例变量至少在本类范围中有效 实例变量与局部变量冲突时,局部变量优先 类中方法定义类似于函数定义 修饰符 返回值类型 方法名( ...
- C++:string类的使用
类 <string> std::string String类的定义 , 其也是个模板类 typedef basic_string<char> string; String cl ...
- (转)C++——std::string类的引用计数
1.概念 Scott Meyers在<More Effective C++>中举了个例子,不知你是否还记得?在你还在上学的时候,你的父母要你不要看电视,而去复习功课,于是你把自己关在房间里 ...
- C++string类总结
一.string的初始化 首先,为了在程序中使用string类型,必须包含头文件 <string>.如下: #include <string> 注意这里不是string.h,s ...
- 【C++】C++string类总结
一.string的初始化 首先,为了在程序中使用string类型,必须包含头文件 <string>.如下: #include <string> 注意这里不是string.h,s ...
随机推荐
- 如何使用Json-lib
数组与List.Collection等都用JSONArray解析 boolean[] boolArray = new boolean[]{true,false,true}; JSONArray jso ...
- spring mvc 和mybatis整合 的异常处理
1.自定义异常信息类 通过构造函数来实现异常信息的接收 public class CustomException extends Exception { //异常信息 private String m ...
- JavaScript入门介绍(二)
JavaScript入门介绍 [函数] 函数function 是Javascript的基础模块单元,用于代码的复用.信息影藏和组合调用. function a(){} 函数对象Function Lit ...
- php 接口 implements 使用
主要对类名,类所拥有的方法,以及所传参数起约束和规范做用,感觉跟php abstract 抽象类又有点像. 一,接口的定义和调用 <?php interface face1 { const pa ...
- Spring Mvc Controller间跳转 重定向 传参 (转)
原文链接:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ 1. 需求背景 需求:spring MVC框架con ...
- js 获取时间 new Date()详细介绍
javaScript系列:js中获取时间new Date()详细介绍 (2012-03-31 09:54:25) 转载▼ 标签: js时间 new date() 字符类型 转换 分类: study-j ...
- XML3_XML元素和节点的具体解释
就像一个树状的目录.可以把第一行当作它扎根的“土地”.XML文件是由节点构成的.它的第一个节点为“根节点”.一个XML文件必须有且只能有一 个根节点,其他节点都必须是它的子节点.我们在FLASH里使用 ...
- Python之路----数据类型
Python的集成开发环境(IDE):pycharm 数据类型 数字 整数int(integer) 浮点数float 布尔型,只有两个值 真:True 假:False 字符串 列表 元组 字典 一.字 ...
- git push用法和常见问题分析
在使用git 处理对android的修改的过程之中总结的.但不完善 Git push $ git push origin test:master // 提交本地test分支作为远程的m ...
- POJ 1716 Integer Intervals 差分约束
题目:http://poj.org/problem?id=1716 #include <stdio.h> #include <string.h> #include <ve ...