首页 > 代码库 > string类的实现

string类的实现

mystring.h

#ifndef _MYSTRING_H_
#define _MYSTRING_H_

#include <stddef.h>
#include <iostream>

class mystring
{
    friend std::ostream &operator<<(std::ostream &os, const mystring &);
    friend std::istream &operator>>(std::istream &is, mystring &);

    friend mystring operator+(const mystring &, const mystring &);
    friend mystring operator+(const char *, const mystring &);
    friend mystring operator+(const mystring &, const char *);

    friend bool operator<(const mystring &, const mystring &);
    friend bool operator<=(const mystring&, const mystring &);
    friend bool operator>(const mystring&, const mystring &);
    friend bool operator>=(const mystring&, const mystring &);
    friend bool operator==(const mystring&, const mystring&);
    friend bool operator!=(const mystring&, const mystring &);
public:
    mystring();
    mystring(const char*);
    mystring(const mystring&);
    ~mystring();

    mystring &operator=(const mystring&);
    mystring &operator=(const char *);

    mystring &operator+=(const mystring&);
    mystring &operator+=(const char *);

    char &operator[](size_t);
    char operator[](size_t) const;
    void debug() const;
    size_t size() const;
    const char *c_str() const;
private:
    char *str_;
};

inline std::ostream &operator<<(std::ostream &os, const mystring &s)
{
    os << s.str_;
}

inline std::istream &operator>>(std::istream &is, mystring &s)
{
    char buf[1024] = {0};
    is >> buf;
    if(is){
        s = buf;
        return is;
    }
}

inline mystring operator+(const mystring &s1, const mystring &s2)
{
    mystring ret(s1);
    ret += s2;
    return ret;
}

inline mystring operator+(const mystring &s1, const char *s2)
{
    return s1 + mystring(s2);
}

inline mystring operator+(const char *s1, const mystring s2)
{
    return mystring(s1) + s2;
}


#endif /*MYSTRING_H*/ 


mystring.cpp

#include "mystring.h"
#include <iostream>
#include <string.h>
using namespace std;

    mystring::mystring()
:str_(new char[1])
{
    str_[0] = '\0';
}

    mystring::mystring(const char *s)
:str_(new char[strlen(s) + 1] )
{
    ::strcpy(str_, s);
}

    mystring::mystring(const mystring &s)
:str_(new char[s.size() + 1])
{
    ::strcpy(str_, s.str_);
}

mystring::~mystring()
{
    delete []str_;
}

mystring &mystring::operator=(const char *s)
{
    if(str_ != s)
    {
        delete []str_;
        str_ = new char[::strlen(s) + 1];
        ::strcpy(str_, s);
    }
    return *this;
}

mystring &mystring::operator=(const mystring &s)
{
    if(this != &s)
    {
        delete []str_; 
        str_  = new char[strlen(s.str_) + 1];
        ::strcpy(str_, s.str_);
    }
    return *this;
}

mystring &mystring::operator+=(const mystring &s)
{
    char *pt = new char[size() + s.size() + 1];
    ::strcpy(pt, str_);
    ::strcat(pt, s.str_);

    delete []str_;
    str_ = pt;

    return *this;
}

mystring &mystring::operator+=(const char *s)
{
    return  operator+=(mystring(s));
}

void mystring::debug() const
{
    cout << str_ << endl;
}

size_t mystring::size() const
{
    return strlen(str_);
}

const char *mystring::c_str() const 
{
    return str_;
}

bool operator<(const mystring &s1, const mystring &s2)
{
    return ::strcmp(s1.str_, s2.str_) < 0;
}

bool operator>(const mystring &s1, const mystring &s2)
{
    return operator<(s2, s1);
}

bool operator<=(const mystring &s1, const mystring &s2)
{
    return !(s1 > s2);
}

bool operator>=(const mystring &s1, const mystring &s2)
{
    return !(s1 < s2);
}

bool operator==(const mystring &s1, const mystring &s2)
{
    return ::strcmp(s1.str_, s2.str_) == 0;
}

bool operator!=(const mystring &s1, const mystring &s2)
{
    return !(s1 == s2);
}

char &mystring::operator[](size_t index) 
{
    return str_[index];
}

char mystring::operator[](size_t index) const
{
return str_[index];
}


string类的实现