4.串 c++类

串,即字符串( String)是由零个或多个字符组成的有限序列。

子串:串中任意个连续的字符组成的子序列。 主串:包含子串的串。
于待在主串中的位置:字符在串中的序号。

实现

String.h
#ifndef _STRING_H_
#define _STRING_H_


class String
{
public:
    friend std::ostream &operator<<(std::ostream &out, String &s);
    friend std::istream &operator>>(std::istream &in, String &s);

public:
    String();
    String(const char *ch);
    String(const String &s);
    ~String();

public:
    String &operator=(const String &s);
    String operator+(const String &s);
    String &operator=(const char *ch);
    String &operator+(const char *ch);

public:
    char *getD();
    void Print();
    int size();
    String substr(int ed);
    String substr(int be, int ed);

private:
    char *data;
    int length;
};


#endif //_STRING_H_
String.cpp
#include "String.h"
#include <string.h>
#include <iostream>


String::String()
{
    length = 0;
    data = new char[1];
    data[0] = '\0';
}

String::String(const char *ch) //字符数组拷贝
{
    length = strlen(ch);
    data = new char[length+1];
    strcpy(data, ch);
}

String::String(const String &s) //对象拷贝
{
    // 1.申请
    // 2.赋值
    int len = s.length;
    length = len;
    data = new char[len];
    strcpy(data, s.data);
}

String::~String()
{
    length = 0;
    delete[] data;
    data = NULL;
}

int String::size()
{
    return length;
}

char *String::getD()
{
    return data;
}

String &String::operator=(const String &s)
{
    //防止存不下先释放
    if(this->data != NULL)
    {
        delete[] this->data;
        this->data = NULL;
    }
    //申请
    this->data = new char[s.length];
    //赋值
    strcpy(this->data, s.data);
    length = s.length;
    return *this; //返回对象本身
}

String String::operator+(const String &s)
{
    String temp = this->data;
    strcat(temp.data, s.data);
    temp.length = length+s.length;
    return temp;
}

std::ostream &operator<<(std::ostream &out, String &s)
{
    int len = s.length;
    for(int i = 0; i < len; ++ i)
		out << s.data[i];
	return out; 
}

std::istream &operator>>(std::istream &in, String &s)
{
    char *ch = new char[1000];
    in >> ch;
    if(!in)
    {
        s = String();
    }
    s = String(ch);
    delete[] ch;
    return in;
}

String &String::operator=(const char *ch)
{
    if(this->data != NULL)
    {
        delete[] this->data;
        this->data = NULL;
    }
    int len = strlen(ch);
    this->data = new char[len+1];
    this->length = len;
    strcpy(this->data, ch);
    return *this;
}
String &String::operator+(const char *ch)
{
    String temp = this->data;
    if(this->data != NULL)
    {
        delete[] this->data;
        this->data = NULL;
    }
    int len = strlen(ch);
    this->data = new char[len+1+length];
    strcpy(this->data, temp.data);
    strcat(this->data, ch);
    length = length+len;
    return *this;
}

void String::Print()
{
    for(int i = 0; i < strlen(data); ++ i)
        std::cout << data[i];
    std::cout << std::endl;
    std::cout << strlen(data) << " " << length;
}

字串匹配

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

// 朴素模式匹配算法
int getIndex(string S, string T)
{
    int i = 0, j = 0;
    int slen = S.length(), tlen = T.length();
    while(i < slen && j < tlen)
    {
        if(S[i] == T[j])
        {
            ++ i; ++ j;
        }
        else 
        {
            i = i-j+1;
            j = 0;
        }
    }
    if(j >= tlen)
        return i-tlen+1;
    else 
        return -1;
}
/*
    串的前缀:包含第一个字符,且不包含最后一个字符的子串
    串的后缀:包含最后一个字符,且不包含第一个字符的子串
*/
// next数组可以优化
void getNext(int next_j[], string s)
{
    int i = 0, j = -1;
    int len = s.length();
    next_j[0] = -1;
    while(i < len)
    {
        if(j == -1 || s[i] == s[j])
        {
            ++ i; ++ j;
            next_j[i] = j;
        }
        else 
        {
           j = next_j[j];
        }
    }
}
int getIndex_KMP(string S, string T, int next_j[])
{
    int i = -1, j = -1;
    int slen = S.length(), tlen = T.length();
    while(i < slen && j < tlen)
    {
        if(j == 0 || S[i] == T[j])
        {
            ++ i; ++ j;
        }
        else 
        {
            j = next_j[j];
        }
    }
    if(j >= tlen)
        return i-tlen+1;
    else
        return -1;
}


int main()
{
    string s1 = "wangdao";
    string s2 = "gda";
    int n[100] = {0};
    string ss = "ababde";
    getNext(n, ss);
    cout << getIndex_KMP(s1, s2, n) << endl;
    system("pause");
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值