鱼C论坛

 找回密码
 立即注册
查看: 1447|回复: 2

[技术交流] 自己写的string类,不太成熟(可以用自己的iostream)

[复制链接]
发表于 2014-2-15 11:41:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 andalousie 于 2014-2-23 09:45 编辑

我的iostream(ios.h)下面是我的string(str.h)
#ifndef STD_STRING
#define STD_STRING

#include "ios.h"
#include <string.h>

void memmove(char *dest, char *src, size_t n)
{
    /* No need to do that thing. */
    if (dest == src)
        return;

    /* Check for destructive overlap.  */
    if (src < dest && dest < src + n) {
        /* Destructive overlap ... have to copy backwards.  */
        src += n;
        dest += n;
        while (n-- > 0)
            *--dest = *--src;
    } else {
        /* Do an ascending copy.  */
        while (n-- > 0)
            *dest++ = *src++;
    }
}

class out_of_range{
public:
        out_of_range()
        {
                std::cerr << "Error: Invalid access of element. " << std::endl;
        }
};

namespace std{
        class string{
        public:
                typedef size_t        size_type;
                typedef char          value_type;
                typedef const string& const_reference;
                typedef const char *  const_pointer;
                
            string(const_pointer str = "");                 //default constructor(value_type)
            string(value_type);
            string(const_reference);                  //assign constructor(string)
            
            string&    operator= (const_reference);         //operator=
            string     operator+ (const_reference) const;   //operator+
            string&    operator+=(const_reference);         //operator+=
            
            //bool operators
            bool       operator==(const_reference) const;    //operator==
            bool       operator==(const_pointer) const;
            bool       operator!=(const_reference) const;    //operator!=
            bool       operator!=(const_pointer) const;
            bool       operator< (const_reference) const;    //operator<
            bool       operator< (const_pointer) const;
            bool       operator> (const_reference) const;    //operator>
            bool       operator> (const_pointer) const;
            bool       operator<=(const_reference) const;    //operator<=
            bool       operator<=(const_pointer) const;
            bool       operator>=(const_reference) const;    //operator>=
            bool       operator>=(const_pointer) const;
            
            value_type&      operator[](const size_type) const;  //operator[]
            value_type&      at(const size_type n) const;
            
            size_type  size() const { return _len; }
            size_type  length() const { return _len; }
            string&    erase(size_type pos, size_type len);
            void       swap(string& str);
            void       clear();
            bool       empty()  { return !_len; }
            const_pointer c_str() const { return const_cast<char*>(m_data); }
            ~string(void);
        private:
            value_type *m_data;
            size_type  _len;
            mutable bool local;
        };
        
        inline string::string(const_pointer str)
        {
                _len   = strlen(str);
        m_data = new value_type[_len+1];
        strcpy(m_data, str);
        local  = true;
        }
        
        inline string::string(value_type ch)
        {
            m_data = new value_type[1];
        m_data[0] = ch;
        _len   = 1;
        local  = true;
        }
        
        inline string::string(const_reference other)
        {
                local  = false;
            m_data = other.m_data;
            _len   = other._len;
        }
        
        inline string::~string(void)
        {
                if(local) delete [] m_data;
        }
        
        inline string& string::operator=(const_reference other)
        {
            if (this!=&other)
            {
                string tmp(other);
                swap(tmp);
            }
            return *this;
        }
        
        inline string string::operator+(const_reference other)const
        {
            string newstring;
            newstring.local = false;
                newstring.m_data = new value_type[strlen(m_data)+strlen(other.m_data)+1];
             strcpy(newstring.m_data,m_data);
            strcat(newstring.m_data,other.m_data);
            newstring._len = _len + other._len;
             return newstring;
        }
        
        inline string& string::operator+=(const_reference other)
        {
            *this = *this + other;
            return *this;
        }
        
        inline bool string::operator==(const_reference s) const
        {
             return s._len == _len
                   && !strcmp(m_data,s.m_data);
        }
        
        inline bool string::operator==(const_pointer s) const
        {
             return strlen(s) == _len
                   && !strcmp(m_data,s);
        }
        
        inline bool string::operator!=(const_reference s) const
        {
             return !(*this == s);
        }
        
        inline bool string::operator!=(const_pointer s) const
        {
             return !(*this == s);
        }
        
        inline bool string::operator< (const_reference s) const
        {
             return strcmp(m_data,s.m_data) < 0;
        }
        
        inline bool string::operator< (const_pointer s) const
        {
             return strcmp(m_data,s) < 0;
        }
        
        inline bool string::operator> (const_reference s) const
        {
             return strcmp(m_data,s.m_data) > 0;
        }
        
        inline bool string::operator> (const_pointer s) const
        {
             return strcmp(m_data,s) > 0;
        }
        
        inline bool string::operator<=(const_reference s) const
        {
             return !(*this > s);
        }
        
        inline bool string::operator<=(const_pointer s) const
        {
             return !(*this > s);
        }
        
        inline bool string::operator>=(const_reference s) const
        {
             return !(*this < s);
        }
        
        inline bool string::operator>=(const_pointer s) const
        {
             return !(*this < s);
        }
                
        inline char& string::operator[](const size_type n) const
        {
             if (n<0 || n>=_len) throw out_of_range();
            return m_data[n];
        }
        
        inline char& string::at(const size_type n) const
        {
             if (n<0 || n>=_len) throw out_of_range();
            return m_data[n];
        }
        
        inline string& string::erase(size_type pos, size_type len)
        {
                if (pos >= _len || 0 == len) return *this;
                const size_type remainder = pos + len;
                if (remainder >= _len || remainder < pos)
                {
                *(m_data + pos) = '\0';
                _len = pos;
                return *this;
            }
            size_type left = _len - remainder + 1;
            value_type *d  = m_data + pos;
            value_type *s  = m_data + remainder;
            memmove(d, s, left);
            _len -= len;
            return *this;
        }
        
        inline void string::swap(string& str)
        {
                string tmp(str);
                
                str.local  = local;
                str.m_data = m_data;
                str._len   = _len;
                
                local  = tmp.local;
                m_data = tmp.m_data;
                _len   = tmp._len;
        }
        
        inline void string::clear()
        {
                m_data = new value_type[1];
                strcpy(m_data, "");
                _len   = 0; 
        }
        
        istream& operator>>(istream& is, string& str)
        {
            char buffer[1000];
            is >> buffer;
            str = buffer;
             return is;
        }
        
        istream & getline(istream & is, string & str)
        {
            char buffer[1000];
            is.getline(buffer, 1000);
            str = buffer;
            return is;
        }
        
        ostream& operator<<(ostream& os, string& str)
        {
            os << str.c_str();
            return os;
        }
        
} //namespace std

#endif
测试文件test_string.cpp
#include "str.h"
using namespace std;

int main()
{
        string s1;
        string s2("23345sghehst");
        cout << s1.size();
        cout << s2.size() << endl;
        s2.erase(4, 3);
        string s3 = s1 + s2;
        cout << s3 << endl;
        if(s1 >= s2) cout << 1;
}



想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-2-18 10:52:43 | 显示全部楼层
路过看看= =!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-2-19 18:09:13 | 显示全部楼层
路过请帮我检查一下代码好吗?我刚刚增添了几个成员函数。这是我写的最长的头文件了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-10 23:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表