C++自定義String字符串類,支持子串搜索


C++自定義String字符串類

實現了各種基本操作,包括重載+號實現String的拼接

findSubStr函數,也就是尋找目標串在String中的位置,用到了KMP字符串搜索算法。

#include <iostream>
#include <cstring>
using namespace std;

class String;

class Data{                                                                     // 抽象基類Data
public:
    virtual const int compareTo(const String& target) const = 0;                // virtual比較函數
};

class String: public Data{                                                      // String類。繼承自Data
public:
    static int num_strings;                                                     // 程序周期內創建的String對象個數
    static const int CINLIM = 80;                                               // 限制字符長度
    String();                                                                   // 默認構造函數
    String(unsigned int capacity);                                              // 以大小為capacity來初始化
    String(unsigned int capacity, char ch);                                     // 填充字符為ch,大小為capacity
    String(const char* target);                                                 // 以一個c風格字符串初始化
    String(const String& st);                                                   // 拷貝構造函數
    ~String(){ delete ptr; }                                                    // 析構函數
    const int       findSubStr(const char* substr) const;                       // 找出子串位置
    const int       findChar(char target) const;                                // 找出字符第一次出現位置
    const int       length() const;                                             // 返回字符串大小
    static int      howManyExit();                                              // 返回程序周期內創建的String對象個數
    const int       compareTo(const String& target) const override;             // 與其它String對象比較大小
    String&         operator=(const String&);                                   // 重載賦值操作符
    String&         operator=(const char*);                                     // 重載賦值操作符
    friend String   operator+(const String&,const String&)      ;               // 重載加號
    char&           operator[](int i);                                          // 重載[]
    const char&     operator[](int i) const;                                    // 重載[]
    friend bool     operator>(const String& st1,const String& st2);             // 重載>
    friend bool     operator<(const String& st1,const String& st2);             // 重載<
    friend bool     operator==(const String& st1,const String& st2);            // 重載==
    friend ostream& operator<<(ostream& os,const String& st);                   // 重載<<
    friend istream& operator>>(istream& is,String& st);                         // 重載>>
private:
    char* ptr;                                                                  // 內置char數組指針
    unsigned int len;                                                           // 當前String長度
};

int String::num_strings = 0;                                                    // 靜態變量初始化

String::String() { ptr = new char[10]; len = 10; }

String::String(unsigned int capacity){
    ptr = new char[capacity]; len = capacity;
    ++ num_strings;
}

String::String(unsigned int capacity, char ch){
    ptr = new char[capacity]; len = capacity;
    for(int i=0;i<len;i++)
        ptr[i] = ch;
    ++ num_strings;
}

String::String(const char* target){
    int tmp = (int)strlen(target);
    len = (unsigned)tmp;
    ptr = new char[len];
    strcpy(ptr,target);
    ++ num_strings;
}

String::String(const String& st){
    int tmp = (int)strlen(st.ptr);
    len = (unsigned)tmp;
    ptr = new char[len];
    strcpy(ptr,st.ptr);
    ++ num_strings;
}

const int String::findSubStr(const char* substr) const{
    int next[100];
    next[0] = -1;
    int i=0,j=-1;
    int lenP = (int)strlen(substr);
    while(i<lenP){
        if(j==-1||substr[i]==substr[j]){
            ++i; ++j;
            next[i] = j;
        }else
            j = next[j];
    }
    i = 0,j = 0;
    while(i<len&&j<lenP){
        if(j==-1|ptr[i]==substr[j]){
            ++i; ++j;
        }else
            j = next[j];
    }
    if(j==lenP) return len - j;
    return -1;
}

const int String::findChar(char target) const{
    for(int i=0;i<len;i++)
        if(ptr[i] == target) return i;
    return -1;
}

const int String::length() const{
    return this->len;
}

int String::howManyExit(){
    return num_strings;
}

const int String::compareTo(const String& target) const{
    if(target.ptr == nullptr || strcmp(ptr,target.ptr)>0)
        return 1;
    else if(strcmp(ptr,target.ptr)==0)
        return 0;
    else
        return -1;
}

String& String::operator=(const String& str){
    if(this == &str)
        return *this;
    delete ptr;
    len = (int)strlen(str.ptr);
    ptr = new char[len];
    strcpy(ptr,str.ptr);
    return *this;
}

String& String::operator=(const char* str){
    delete[] ptr;
    len = (int)strlen(str);
    ptr = new char[len+1];
    strcpy(ptr,str);
    return *this;
}

String operator+(const String& st1,const String& st2){
    int lenSt2 = (int)strlen(st2.ptr);
    int lenSt1 = (int)strlen(st1.ptr);
    char* temp = new char[lenSt1+lenSt2+1];
    strcpy(temp,st1.ptr);
    for(int i=lenSt1;i<lenSt1+lenSt2;i++)
        temp[i] = st2.ptr[i-lenSt1];
    return String(temp);
}

char& String::operator[](int i){
    return ptr[i];
}
const char& String::operator[](int i) const{
    return ptr[i];
}

bool operator<(const String& st1,const String& st2){
    return (std::strcmp(st1.ptr,st2.ptr)<0);
}
bool operator>(const String& st1,const String& st2){
    return (std::strcmp(st1.ptr,st2.ptr)>0);
}
bool operator==(const String& st1,const String& st2){
    return (std::strcmp(st1.ptr,st2.ptr)==0);
}
ostream& operator<<(ostream& os,const String& st){
    os<<st.ptr;
    return os;
}
istream& operator>>(istream& is,String& st){
    char temp[String::CINLIM];
    is.get(temp,String::CINLIM);
    if(is)
        st = temp; //在此處" = "已被重載過了
    while(is && is.get()!='\n')
        continue;
    return is;
}
int main()
{
    String A = "abcd";
    String B = "efgh";
    String C = A + B;
    cout << C << endl;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM