码迷,mamicode.com
首页 > 其他好文 > 详细

使用alloctor类来实现string类

时间:2016-07-12 23:28:58      阅读:329      评论:0      收藏:0      [点我收藏+]

标签:

虽然以前做过更复杂的各种数据结构,不过那只是在看完c++prime7章后做的,没有考虑到类的拷贝体现出来是类值还是类指针,于是写了一些半成品类,不过那些主要是练数据结构,不想再改,于是就想办法模仿了下string,以前都是使用new和delete,虽然简单,但是不够灵活,于是就刻意使用alloctor类来管理内存,这样可以将“内存分配”与“对象构造分开”,因为对象并不是指针这类管理外部资源的对象,于是我就干脆不destory,这样使用alloctor效率优势更加有体现,练下手熟,总的来说没什么难度。

技术分享
#include <iostream>
#include <string>
#include <memory>
using namespace std;
const int incerment=10;
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 = 0; s[i]; i++)
            push_back(s[i]);
    }
    String(const String &s) :elements(nullptr), last_element(nullptr), space_end(nullptr){
        elements = alloc.allocate(s.size() * 2);
        last_element = uninitialized_copy(s.begin(), s.end(), elements);
        space_end = elements + s.size() * 2;
        uninitialized_fill(last_element, space_end, 0);
    }
    String(const size_t &len) :elements(nullptr), last_element(nullptr), space_end(nullptr){
        elements = alloc.allocate(len);
        uninitialized_fill_n(elements, len, 0);
        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*2);
        uninitialized_fill_n(elements, n, ch);
        last_element = elements + n;
        space_end = elements + n * 2;
    }
    ~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, 0);
        }
        *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 = 0;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,0);
        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()+3,b.end()), e(12,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[0] <<"    "<< h[h.size() - 1];
    }
    _CrtDumpMemoryLeaks();
}
View Code

 

使用alloctor类来实现string类

标签:

原文地址:http://www.cnblogs.com/Dadio/p/5665076.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!