在一些C++笔试题里,会有这样一道题,那就是让你自己实现一个简单的string类。自己在面试的时候就遇到过这个题。在这里说一下自己是怎么做的。
1.编写头文件
主要包含一些基本的操作,构造函数、拷贝构造函数和析构函数。
#pragma once
#include <iostream>
using namespace std;
class MyString
{
public:
MyString(int len = 0);//构造函数
MyString(const char *p);//构造函数
MyString(const MyString& s);//拷贝构造函数
~MyString();//析构函数
private:
int m_len;//数组长度
char *m_p;//指针
};
2.cpp文件
#include"stdafx.h"
#include"MyString.h"
MyString::MyString(int len)
{
if (len == 0)//长度为0是赋值为""
{
m_len = 0;
m_p = new char[m_len + 1];
strcpy(m_p, "");
}
else//长度不为空时初始化新申请的内存
{
m_len = len;
m_p = new char[m_len + 1];
memset(m_p, 0, m_len);
}
}
MyString::MyString(const char* p)
{
if (p == NULL)//当指针为空时,赋值为""
{
m_len = 0;
m_p = new char[m_len + 1];
strcpy(m_p, "");
}
else//不为空时将p指向的内容赋值给新开辟的内存
{
m_len = strlen(p);
m_p = new char[m_len + 1];
strcpy(m_p, p);
}
}
MyString::MyString(const MyString& s)//拷贝构造函数
{
m_len = s.m_len;
m_p = new char[m_len + 1];
strcpy(m_p, s.m_p);
}
MyString::~MyString()//析构函数
{
if (m_p != NULL)
{
delete[] m_p;
m_p = NULL;
m_len = 0;
}
}
到这里简单的string类就实现了,接下来是一些运算符的重载操作。
3.头文件(新增了一些重载操作)
#pragma once
#include <iostream>
using namespace std;
class MyString
{
public:
MyString(int len = 0);//构造函数
MyString(const char *p);//构造函数
MyString(const MyString& s);//拷贝构造函数
~MyString();//析构函数
public: //重载=号操作符以及[],+
MyString& operator=(const char *p);
MyString& operator=(const MyString &s);
char& operator[] (int index);
MyString operator+(const MyString &s) const;
public: //重载 == !==
bool operator==(const char *p) const;
bool operator==(const MyString& s) const;
bool operator!=(const char *p) const;
bool operator!=(const MyString& s) const;
public: //重载<>
int operator<(const char *p);
int operator>(const char *p);
int operator<(const MyString& s);
int operator>(const MyString& s);
private:
int m_len;//数组长度
char *m_p;//指针
};
4.cpp文件(增加重载操作)
//重载等号=操作符和[]
MyString& MyString::operator=(const char*p)
{
if (m_p != NULL)
{
delete[] m_p;
m_len = 0;
}
if (m_p == NULL)
{
m_len = 0;
m_p = new char[m_len + 1];
strcpy(m_p, "");
}
else
{
m_len = strlen(p);
m_p = new char[m_len + 1];
strcpy(m_p, p);
}
return *this;
}
MyString& MyString::operator=(const MyString& s)
{
if (m_p != NULL)
{
delete[] m_p;
m_len = 0;
}
m_len = s.m_len;
m_p = new char[m_len + 1];
strcpy(m_p, s.m_p);
return *this;
}
char& MyString::operator[](int index)
{
return m_p[index];
}
MyString MyString::operator+(const MyString &s) const
{
MyString newString;
newString.m_len = m_len + s.m_len;
newString.m_p = new char[newString.m_len + 1];
strcpy(newString.m_p, m_p);
strcat(newString.m_p, s.m_p);
return newString;
}
//重载 == !=
bool MyString::operator==(const char* p) const
{
if (p == NULL)
{
if (m_len == 0)
{
return true;
}
else
{
return false;
}
}
else
{
if (m_len == strlen(p))
{
return !strcmp(m_p, p);
}
else
{
return false;
}
}
}
bool MyString::operator==(const MyString& s)const
{
if (s.m_len != m_len)
{
return false;
}
else
{
return !strcmp(m_p, s.m_p);
}
}
bool MyString::operator!=(const char* p)const
{
return !(*this == p);
}
bool MyString::operator!=(const MyString& s)const
{
return !(*this == s);
}
//重载<>
int MyString::operator>(const char* p)
{
return strcmp(p, this->m_p);
}
int MyString::operator<(const char* p)
{
return strcmp(this->m_p, p);
}
int MyString::operator>(const MyString& s)
{
return strcmp(s.m_p, this->m_p);
}
int MyString::operator<(const MyString& s)
{
return strcmp(this->m_p, s.m_p);
}
至此一个由自己编写的简单string类就实现了。