zoukankan      html  css  js  c++  java
  • string的实现

    面试常常用到string类的实现,自己总结了一下:

      

    #pragma once
    
    #include <iostream>
    #include <cassert>
    #include <iomanip>
    using namespace std;
    
    class string{
    
        friend ostream& operator <<(ostream&,string&); 
        friend istream& operator >>(istream&,string&);
    
    public:
        string(const char* str = NULL);
        //assignment constructor 
        string(const string& str); 
        ~string(){
            delete [] m_data;
        }
    
        //get the length of string
        int size(){
            return mystrlen(m_data);
        }
        //return substring  这里存在一些问题待改善
        //当字符串的长度稍大一些时会出错;
        string substr(int pos);
        string substr(int pos,int len);
        //overload  operator =
        string& operator =(const string& other);
        //overload operator +
        string operator +(const string& other)const;
        //overload operator +=
        string& operator +=(const string& other);
        //overload operator ==
        bool operator ==(const string& other)const;
        //overload operator []
        char& operator [](unsigned int);
    
    private:
        char* m_data;
    };

    cpp

    #include "string.h"
    
    string::string(const char* str /* = NULL */)
    {
        if (!str)
        {
            m_data = new char[1];
            *m_data = '';
        } 
        else
        {
            int nLen = strlen(str);
            m_data = new char[nLen + 1];
            strcpy(m_data,str);
        } 
    }
    
    string::string(const string& str)
    {
        
        m_data = new char[strlen(str.m_data)+1];
        strcpy(m_data,str.m_data);
    }
    
    ostream& operator <<(ostream& oscout,string& str)
    {
        oscout<<str.m_data;
        return oscout;
    }
    
    istream& operator >>(istream& iscin,string& str)
    {
        char temp[255];
        iscin>>setw(255)>>temp;
        str = temp;
        return iscin;
    }
    
    string string::substr(int pos)
    {
        /*
        int nLen = strlen(m_data);
        char* res = new char(nLen - pos +1);
        char* src = m_data + pos;
        strncpy(res,src,nLen-pos);
        string strres(res);
        return strres;
        */
    
        int nLen = strlen(m_data);
        if (nLen < pos)
        {
            pos = nLen;
        }
        int nCopyLen = nLen - pos;
        char* res = new char(nLen - pos +1);
        char* dest =  res;
        char* src = m_data + pos;
    
        for (int i = 0;i < nCopyLen; i++)
        {
            *dest++ = *src++;
        }
    
        *dest = '';
        string strres(res);
        return strres;
    }
    
    string string::substr(int pos,int len)
    {
        int maxlen = strlen(m_data)-pos;
        if (maxlen < 0) maxlen = 0;
        int newLen = maxlen < len ? maxlen : len;
        char* res = new char(newLen+1);
        char* src = m_data + pos;
        strncpy(res,src,newLen);
        *(res+newLen) = ''; 
        string strres(res);
        return strres;
    }
    
    string string::operator +(const string& other)const
    {
        string newString;  
        if(!other.m_data)  
            newString = *this;  
        else if(!m_data)  
            newString = other;  
        else  
        {  
            newString.m_data = new char[strlen(m_data)+strlen(other.m_data)+1];  
            strcpy(newString.m_data,m_data);  
            strcat(newString.m_data,other.m_data);  
        }  
        return newString; 
    }
    
    string& string::operator =(const string& other)
    {
        if (this==&other) 
            return *this;
         
        delete [] m_data;  
    
        m_data = new char[strlen(other.m_data)+1];  
        strcpy(m_data,other.m_data);  
        
        return *this;
    }
    
    string& string::operator +=(const string& other)
    {
    
        char* data = new char[strlen(m_data)+strlen(other.m_data)+1];
        strcpy(data,m_data);
        delete [] m_data;
        m_data = data;
        strcat(m_data,other.m_data);
        
        return *this;
    }
    
    //overload operator ==
    bool string::operator ==(const string& other)const
    {
        if ( strlen(other.m_data) != strlen(m_data) )  
            return false;  
        return strcmp(m_data,other.m_data)?false:true; 
    }
    //overload operator []
    char& string::operator [](unsigned int index)
    {
        if (index >= 0 && index <= strlen(m_data))  
            return m_data[index];
    
        return m_data[strlen(m_data)+1];
    }

    另外转置输出string例子

    #include <iostream>
    #include <string>
    using namespace std;
    
    
    
    //只限英文字符
    void ReverseString(string& str) 
    {
        int nLen = str.size();
    
        for (int i = 0; i < nLen/2; i++)
        {    
            char cTmp = str[nLen-i-1];
            str[nLen-i-1] = str[i];
            str[i] = cTmp;
        }
    }
    //中英字符
    void ReverseString1(string& str)
    {
        int nLen = str.size();
        
        string strTmp;
        strTmp.resize(nLen,' ');
    
        for (int i =0; i < nLen; i++)
        {    
            if ( str[i] < 255 && str[i] > 0)
            {
                strTmp[nLen-i-1] = str[i];
            } 
            else
            {
                strTmp[nLen-i-2] = str[i];
                strTmp[nLen-i-1] = str[i+1];
                ++i;
            }
        }
        str = strTmp;
    }
    
    
    int main(int argc, char* argv[])
    {
        int nflag = 1;
    
        while (nflag)
        {
            cout<<"enter a string including both lowercase and upercase letters!"<<endl;
            string str;
            cin.clear();
            cin.sync();
            getline(cin,str);
            ReverseString1(str);
            cout<<str<<endl;
            cout<<"enter 0 to exit or 1 to continue"<<endl;    
            cin>>nflag;
        }
    
        return 0;
    }
  • 相关阅读:
    Java多线程缓存器简单实现
    synchronized Lock用法
    【项目】01CMS的CRUD
    RabbitMQ模式,RabbitMQ和springboot整合,RabbitMQ全链路消息不丢失解决
    FreeMarker在项目中实际运用随感
    自定义异常springMVC&springCloud
    单例设计模式懒汉式和恶汉式
    浅析重不重写hashcode和equals对于HashSet添加元素的影响
    JAVA异常处理原理浅析
    Static(静态)关键字入门
  • 原文地址:https://www.cnblogs.com/hervey/p/4663086.html
Copyright © 2011-2022 走看看