其他分享
首页 > 其他分享> > 4.自己模拟实现一个string

4.自己模拟实现一个string

作者:互联网

main.cpp

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s1("aaa");
	string s2("bbb");
	string s3;

	s3 = s1;
	s3 = "ccc";
	s3 = s1 + s2;
	s3 = s1 + "ddd";
	s3 = "ddd" + s1;
	s3 += s1;
	
	if (s1 > s2) {}
	if (s1 == s2) {}
	cin >> s1;
	cout << s1;
	s1.size();
	cout << s1[3] << endl; //中括号运算符
	//cout << *s1 << endl; // string不支持解引用运算符
	//cout << &s1 << endl; // 取地址符也能重载
	//new delete
	return 0;
}

先明白一个概念:
成员方法的const
成员方法中如果确定不会改变对象中的成员 就给成员方法加上const

如果不确定是否会改变成员,或者对外提供了改变的途径,
就需要实现const和非const的

参数的const
如果参数传入是指针或者引用,且保证不会再函数中改变,就加上const

返回值的const
返回的如果是引用或者指针,且不希望被外界改变,就加上const

mstring.h

#include"mstring.h"

Mstring::Mstring(int len = 10)
{
	_len = len;
	_str = new char[_len];
	memset(_str, 0, _len);
}

Mstring::Mstring(const Mstring& src)
{
	_len = src._len;
	_str = new char[_len];
	memset(_str, 0, _len);
	strcpy(_str, src._str);
	/*
	for (int i = 0; i < src.size(); i++)
	{
		_str[i] = src._str[i];
	}*/
}

Mstring::Mstring(const char* str)
{
	_len = strlen(str);
	_str = new char[_len];
	memset(_str, 0, _len);
	strcpy(_str, str);
}
Mstring::~Mstring()
{
	delete _str;
	_len = 0;
	_str = NULL;
}

Mstring Mstring::operator=(const Mstring& src)
{
	if (&src == this)
	{
		return *this;
	}
	delete _str;
	_len = src._len;
	_str = new char[_len];
	memset(_str, 0, _len);
	strcpy(_str, src._str);
}

Mstring Mstring::operator=(const char* str);

Mstring Mstring::operator+(const Mstring& src)
{
	int len = src.size() + size()+1;
	char* tmp = new char[len];
	memset(tmp, 0, len);

	strcpy(tmp, _str);
	strcpy(tmp + size(), src._str);

	return tmp;
}
Mstring Mstring::operator+(const char* str);

Mstring Mstring::operator==(const Mstring& src)
{
	return strcmp(_str, src._str) == 0;
}
Mstring Mstring::operator==(const char* str);

Mstring Mstring::operator>(const Mstring& src);
Mstring Mstring::operator>(const char* str);

ostream& Mstring::operator<<(ostream& out)
{
	out << _str << endl;
	return out;
}
istream& Mstring::operator>>(istream& in)
{
	char str[1024] = {0};
	in >> str;
	
	delete _str;
	_len = strlen(str);
	_str = new char[_len];
	strcpy(_str, str);
	return in;
}



Mstring operator+(const char* str, const Mstring& src);

Mstring operator==(const char* str, const Mstring& src);
Mstring operator>(const char* str, const Mstring& src);
ostream& operator<<(ostream& out, const Mstring& src);
istream& operator>>(istream& in, const Mstring& src);

mstring.cpp

#include"mstring.h"

Mstring::Mstring(int len = 10)
{
	_len = len;
	_str = new char[_len];
	memset(_str, 0, _len);
}

Mstring::Mstring(const Mstring& src)
{
	_len = src._len;
	_str = new char[_len];
	memset(_str, 0, _len);
	strcpy(_str, src._str);
	/*
	for (int i = 0; i < src.size(); i++)
	{
		_str[i] = src._str[i];
	}*/
}

Mstring::Mstring(const char* str)
{
	_len = strlen(str);
	_str = new char[_len];
	memset(_str, 0, _len);
	strcpy(_str, str);
}
Mstring::~Mstring()
{
	delete _str;
	_len = 0;
	_str = NULL;
}

Mstring Mstring::operator=(const Mstring& src)
{
	if (&src == this)
	{
		return *this;
	}
	delete _str;
	_len = src._len;
	_str = new char[_len];
	memset(_str, 0, _len);
	strcpy(_str, src._str);
}

Mstring Mstring::operator=(const char* str);

Mstring Mstring::operator+(const Mstring& src)
{
	int len = src.size() + size()+1;
	char* tmp = new char[len];
	memset(tmp, 0, len);

	strcpy(tmp, _str);
	strcpy(tmp + size(), src._str);

	return tmp;
}
Mstring Mstring::operator+(const char* str);

Mstring Mstring::operator==(const Mstring& src)
{
	return strcmp(_str, src._str) == 0;
}
Mstring Mstring::operator==(const char* str);

Mstring Mstring::operator>(const Mstring& src);
Mstring Mstring::operator>(const char* str);

ostream& Mstring::operator<<(ostream& out)
{
	out << _str << endl;
	return out;
}
istream& Mstring::operator>>(istream& in)
{
	char str[1024] = {0};
	in >> str;
	
	delete _str;
	_len = strlen(str);
	_str = new char[_len];
	strcpy(_str, str);
	return in;
}

Mstring operator+(const char* str, const Mstring& src);

Mstring operator==(const char* str, const Mstring& src);
Mstring operator>(const char* str, const Mstring& src);
ostream& operator<<(ostream& out, const Mstring& src);
istream& operator>>(istream& in, const Mstring& src);

标签:Mstring,const,string,src,实现,len,char,str,模拟
来源: https://blog.csdn.net/mwt_442396446/article/details/117716296