c 语言 string类型,自己模拟写C++中的String类型实例讲解

下面是模拟实现字符串的相关功能,它包括一下功能:

String(const char * s);//利用字符串来初始化对象

String();      //默认构造函数

String(const String & s);//复制构造函数,利用String类型来初始化对象

~String();      //析构函数

int length();      //返回String类型中字符串的长度

String & operator=(const String & s);//重载=运算符。

String & operator=(const char *);

char & operator[](int i);  //重载【】运算符

const char & operator[](int i) const;

friend bool operator

friend bool operator>(const String & st,const String & st2);

friend bool operator==(const String & st,const String & st2);//重载==运算符,判断两个String对象是否相等

friend ostream & operator<

friend istream & operator>>(istream & is,String & st2);//重载输入函数

static int HowMang()//返回总共生成的String类对象的数目。

String.h:

#ifndef STRING_H_INCLUDED

#define STRING_H_INCLUDED

#include"iostream"

#include

using std::ostream;

using std::istream;

class String{

private:

char * str;

int len;

public:

static int num_strings;

static const int CINLM=80;

String(const char * s);

String();

String(const String & s);

~String();

int length();

String & operator=(const String & s);

String & operator=(const char *);

char & operator[](int i);

const char & operator[](int i) const;

friend bool operator

friend bool operator>(const String & st,const String & st2);

friend bool operator==(const String & st,const String & st2);

friend ostream & operator<

friend istream & operator>>(istream & is,String & st2);

static int HowMang()

{

return num_strings;

}

};

#endif // STRING_H_INCLUDED

String.cpp:

#include

#include"String.h"

#include"string.h"

using namespace std;

int String::num_strings=0;

int String::length()

{

return this->len;

}

String::String(const char * s)

{

len=strlen(s);

str=new char[len+1];

num_strings++;

}

String::String()

{

str=0;

len=0;

num_strings++;

}

String::String(const String & s)

{

num_strings++;

len=strlen(s.str);

str=new char[len+1];

strcpy(str,s.str);

}

String::~String()

{

--num_strings;

delete [] str;

len=0;

}

String & String::operator=(const String & s)

{

if(this==&s)

return *this;

delete [] str;

len=strlen(s.str);

str=new char[len+1];

strcpy(str,s.str);

// num_strings++;

}

String & String::operator=(const char * s)

{

len=strlen(s);

str=new char[len+1];

strcpy(str,s);

// num_strings++;

}

char & String::operator[](int i)

{

return str[i];

}

const char & String::operator[](int i) const

{

return str[i];

}

bool operator

{

if(strcmp(st.str,st2.str)<0)

return true;

else

return false;

}

bool operator>(const String & st,const String & st2)

{

return (st

}

bool operator==(const String & st,const String & st2)

{

if(strcmp(st.str,st2.str)>0)

return true;

else

return false;

}

ostream & operator<

{

os<

return os;

}

istream & operator>>(istream & is,String & st2)

{

char temp[String::CINLM];

is.get(temp,String::CINLM);

if(is)

st2=temp;

while(is&&is.get()!='\n')

continue;

return is;

}

main.cpp:

#include

#include"String.h"

using namespace std;

int main()

{

String name[5];

char temp[10];

int i;

for(i=0;i<5;i++)

{

cin.get(temp,10);

while(cin&&cin.get()!='\n')

continue;

if(!cin&&temp[0]=='\0')//如果是空串的话,cin会为false

break;

else

name[i]=temp;

}

int total=i;

int firs=0,shor=0;

if(total<0)

{

cout<

}else{

for(i=0;i

{

cout<

}

for(i=0;i

{

if(name[i]

firs=i;

if(name[i].length()

shor=name[i].length();

}

}

cout<

cout<

return 0;

}

以上这篇自己模拟写C++中的String类型实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值