myString的部分功能实现
head.h
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
#include<cstring>
#include<string.h>
using namespace std;
class myString
{
private:
char *str; //记录c风格的字符串
int size; //记录字符串的实际长度
public:
//无参构造
myString();
//有参构造
myString(const char *s); //有参构造 string s("hello wirld");
//重定义拷贝赋值函数
myString& operator=(const myString&);
//重定义访问指定字符
char& operator[](int );
//c_str函数
const char* cToString();
//data函数
const char* data();
//判空函数
bool empty();
//size函数
int get_size();
//at函数
char & at(int index);
//二倍扩容
void expand();
//定义capacity函数
int has_capacity();
//定义clear
void myclear();
//push_back
void mypushback(char);
//pop_back
char* mypopback();
//append
void myappend(int ,char);
//重定义+=运算符
myString & operator+=(const myString& );
//重定义+运算符
char* operator+(const myString&);
//定义析构函数
~myString();
};
#endif // MYSTRING_H
source.c
#include "head.h"
//无参构造
myString ::myString():size(10)
{
str = new char[size]; //构造出一个长度为10的字符串
strcpy(str,"");
cout<<"调用无参构造"<<endl;
}
//有参构造
myString ::myString(const char *s) //有参构造 string s("hello wirld");
{
size=strlen(s);
str= new char[size +1];
strcpy(str,s);
cout<<"调用有参构造"<<endl;
}
//重定义拷贝赋值函数
myString& myString :: operator=(const myString& s)
{
strcpy(this->str,s.str);
size=this->size+s.size;
cout<<"拷贝赋值函数"<<endl;
return *this; //返回自身的引用
}
//重定义访问指定字符
char& myString :: operator[](int pos)
{
return str[pos];
}
//at函数
char &myString ::at(int index)
{
if (index >= 0 && index < size)
{
return str[index]; // 返回索引为 index 的元素的引用
}
else
{
// 处理索引越界的情况
throw std::out_of_range("Index out of bounds");
}
}
//c_str函数
const char* myString ::cToString()
{
return this->str;
}
//data函数
const char* myString :: data()
{
char *sstr=&str[0];
return sstr;
}
//判空函数
bool myString :: empty()
{
if(this->size==0)
{
return 1;
}else
{
return 0;
}
}
//size函数
int myString ::get_size()
{
return size;
}
//二倍扩容
void myString ::expand()
{
char *temp;
size*=2;
temp=new char[size];
strcpy(temp,str);
delete [] str;
str =temp;
}
//定义capacity函数
int myString ::has_capacity()
{
return (sizeof(str)*2-1);
}
//定义clear
void myString :: myclear()
{
memset(str,0,sizeof(str));
}
//push_back
void myString :: mypushback(char C)
{
strcat(str,&C);
}
//pop_back
char * myString ::mypopback()
{
str[strlen(str)-1]=0;
return str;
}
//append
void myString ::myappend(int num,char C)
{
for(int i=0;i<num;i++)
{
strcat(str,&C);
}
}
//重定义+运算符
char* myString:: operator+(const myString& s)
{
char *temp;
temp=strcat(this->str,s.str);
return temp;
}
//重定义+=运算符
myString & myString:: operator+=(const myString& s)
{
strcat(this->str,s.str);
size=this->size+s.size;
return *this;
}
//定义析构函数
myString :: ~myString()
{
delete[]str;
cout<<"调用析构函数"<<endl;
}
main.c
#include "head.h"
int main()
{
//调用无参构造
myString s1;
cout << "扩容前size = " << s1.get_size() << " str1 = " << s1.cToString() << endl;
//调用构造的二倍扩容函数
s1.expand();
cout << "扩容后size = " << s1.get_size() << " str1 = " << s1.cToString() << endl;
//调用有参构造
myString s2("hello");
cout << "size = " << s2.get_size() << " str2 = " << s2.cToString() << endl;
//调用at函数
cout << "第一个字符:" << s2.at(0) << endl;
cout << "第二个字符:" << s2.at(1) << endl;
cout << "第三个字符:" << s2.at(2) << endl;
//调用有参构造
myString s3("");
cout << "size = " << s3.get_size() << " str3 = " << s3.cToString() << endl;
//调用empty函数
//判断字符串s2是否为空
if (s2.empty())
cout << "s2字符串为空!" << endl;
else
cout << "s2字符串不为空!" << endl;
//判断字符串s3是否为空
if (s3.empty())
cout << "s3字符串为空!" << endl;
else
cout << "s3字符串不为空!" << endl;
s3 = s2;
cout << "my_size = " << s3.get_size() << " str3 = " << s3.cToString()<< endl;
cout<<"*************************"<<endl;
myString s4;
s4=s3;
cout<<"s4 capacity="<<s4.has_capacity()<<endl;
cout<<"str4= "<<s4.data()<<endl;
cout<<"s4[2]="<<s4[3]<<endl;
cout<<"*************************"<<endl;
s4.mypushback('A');
s4.mypushback('A');
s4.mypushback('A');
cout<<"str4="<<s4.cToString()<<endl;
s4.mypopback();
cout<<"str4="<<s4.cToString()<<endl;
s4.myclear();
cout<<"str4="<<s4.cToString()<<endl;
s4.myappend(3,'B');
cout<<"str4="<<s4.cToString()<<endl;
s4+=s3;
cout<<"str4="<<s4.cToString()<<endl;
cout<<"*************************"<<endl;
myString s5;
s5=s4+s3;
cout<<"str5="<<s5.cToString()<<endl;
cout<<"*************************"<<endl;
return 0;
}