自己实现了下c++ string 下面是源码,先做好备份,哈哈(源码下载:https://code.csdn.net/snippets/652446)
MyString.h文件
#pragma once
#include <iostream>
class MyString
{
public:
MyString();
~MyString();
MyString(const MyString& str);
MyString(char* data);
MyString& operator=(const MyString& str);
MyString operator+(const MyString& str);//这里返回值不能是引用,因为返回的是局部变量,在函数结束后回释放掉,其引用的对象就会指向不确定内存(如果返回非引用值,系统会在函数结束后创建一个临时变量并赋值返回值提供给被调用函数),与拷贝构造函数不同,其返回的是类指针(this)的引用,函数结束后不会释放
bool operator==(const MyString& str);
char operator[](const unsigned int index);
friend std::ostream& operator<<(std::ostream& output,const MyString& str);
friend std::istream& operator>>(std::istream& in,MyString& str);//
MyString subString(const int start, const int end);
int find(const char c);
int findSubStr(const MyString& substr);
int lenght();
private:
char* m_data;
};
MyString.cpp文件
#include "MyString.h"
//#include <stdlib.h>
#include "Sunday.h"
#pragma warning(disable:4996)
//以下为构造函数
MyString::MyString()
{
m_data = new char[1];
*m_data = '\0';
}
MyString::~MyString()
{
if (m_data != NULL)
{
delete[] m_data;
}
}
MyString::MyString(const MyString& str)
{
if (str.m_data == NULL)
{
m_data = new char[1];
*m_data = '\0';
}
else
{
m_data = new char[strlen(str.m_data) + 1];
strcpy(m_data, str.m_data);
}
}
MyString::MyString(char* data)
{
if (data == NULL)
{
m_data = new char[1];
*m_data = '\0';
}
else
{
m_data = new char[strlen(data) + 1];
strcpy(m_data,data);
}
}
//=操作符重写
MyString& MyString::operator=(const MyString& str)
{
if (this != &str)
{
delete[] m_data;
m_data = new char[strlen(str.m_data) + 1];
strcpy(m_data,str.m_data);
}
return *this;
}
//+操作符重写 注意:此处不能返回引用
MyString MyString::operator+(const MyString& str)
{
MyString newString;
if (!str.m_data)
{
newString = *this;
}
else if (!m_data)
{
newString = str;
}
else
{
newString = new char[strlen(m_data)+strlen(str.m_data)+1];
strcpy(newString.m_data,m_data);
strcat(newString.m_data, str.m_data);
}
return newString;
}
//==操作符重写
bool MyString::operator==(const MyString& str)
{
if (strlen(str.m_data) != strlen(m_data))
{
return false;
}
else
{
for (int i = 0; i < strlen(m_data); ++i)
{
if (str.m_data[i] != m_data[i])return false;
}
}
return true;
}
//下标操作符重载写
char MyString::operator[](const unsigned int index)
{
return m_data[index];
}
//计算字符长度函数
int MyString::lenght()
{
//return strlen(m_data) ;//此方法也行
int length=0;
for (; m_data[length] != '\0'; ++length);
return length;
}
//输出流重写
std::ostream& operator<<(std::ostream& output, const MyString& str)
{
return output << str.m_data;
}
//输入流重写
std::istream& operator>>(std::istream& in, MyString& str)
{
char buff[1024];//缓冲区
in >> buff;
if (strcmp(buff, str.m_data) != 0)
{
delete[] str.m_data;//删除后str.m_data的地址不变,反而不删除地址变了,这里是内存分配的使用,局部变量存放到内存栈中,
//删除一个变量后,其地址会被下一个变量地址覆盖
str.m_data = new char[strlen(buff) + 1];
strcpy(str.m_data, buff);
}
return in;
}
MyString MyString::subString(const int start, const int end)
{
int sta = 0, en = 0;
start < end ? (sta = start, en = end) : (sta = end, en = start);
MyString substr;
substr.m_data = new char[en-sta+2];
int i = 0;
for (; sta!= en+1; ++sta, ++i)
{
substr.m_data[i] = m_data[sta];
}
substr.m_data[i] = '\0';
return substr;
}
int MyString::find(const char c)
{
int index = -1;//不存在c则返回-1
for (int i = 0; i < strlen(m_data); ++i)
{
if (m_data[i] == c)
{
index = i;
break;
}
}
return index;
}
int MyString::findSubStr(const MyString& substr)
{
Sunday match;//此处采用sunday 算法来进行模式匹配
return match.find(m_data,substr.m_data);
}
Sunday算法实现,写在一个类中
/*
@参考sunday算法:http://www.cnblogs.com/huhu0013/p/3227838.html
@
*/
#pragma once
#include<iostream>
class Sunday
{
public:
Sunday();
~Sunday();
public:
int find(const char* pattern, const char* text);
private:
void preCompute(const char* pattern);
private:
//Let's assume all characters are all ASCII
static const int ASSIZE = 128;
int _td[ASSIZE];
int _patLength;
int _textLength;
};
#include "Sunday.h"
#pragma waring(disable:4996)
Sunday::Sunday()
{
}
Sunday::~Sunday()
{
}
void Sunday::preCompute(const char* pattern)
{
for (int i = 0; i < ASSIZE; i++)
_td[i] = _patLength + 1;
const char* p;
for (p = pattern; *p; p++)
_td[*p] = _patLength - (p - pattern);
}
int Sunday::find(const char* text,const char* pattern)
{
_patLength = strlen(pattern);
_textLength = strlen(text);
if (_patLength <= 0 || _textLength <= 0)
return -1;
preCompute(pattern);
const char *t, *p, *tx = text;
while (tx + _patLength <= text + _textLength)
{
for (p = pattern, t = tx; *p; ++p, ++t)
{
if (*p != *t)
break;
}
if (*p == 0)
return tx - text;
tx += _td[tx[_patLength]];
}
return -1;
}
最后是main函数:(自己可以添加)
#include <iostream>
#include "MyString.h"
using namespace std;
int main()
{
MyString str("abcdEFg");
MyString str1;
MyString str2("EF");
MyString str3 = str.subString(1, 3);
MyString str4 = str3 + str2;
cout << str3 << endl;
cout << str4 << endl;
cout << str4.find('p') << endl;
cout << str.findSubStr("EF") << endl;
return 1;
}