C++ String类的实现

string头文件:


#ifndef MAIN_WTF_STRING_H
#define MAIN_WTF_STRING_H


#include<iostream>
using namespace std;


namespace main_wtf_5
{
class string
{
private:
char* characters;          //存储字符串数据
size_t alloacted;  //字符串容量
size_t current_length;     //字符串长度


public:

string(const char str[] = "");
string(const string& source);
void operator = (const string& source);


size_t length()const { return current_length; } ;
char* getChar() const { return characters; };
char operator[](size_t position) const;
void operator += (const string& addend);
void operator += (const char addend);
void operator += (const char addend[]);
void reserve(size_t n);


~string();


friend istream& operator >>(istream& ins,string& target);
friend ostream& operator <<(ostream& outs,const string& source);


};


string operator +(const string& s1,const string& s2);
istream& operator >>(istream& ins,string& target);
ostream& operator <<(ostream& outs,const string& source);



bool operator == (const string& s1,const string& s2);
bool operator > (const string& s1,const string& s2);
bool operator < (const string& s1,const string& s2);
bool operator >= (const string& s1,const string& s2);
bool operator <= (const string& s1,const string& s2);
bool operator != (const string& s1,const string& s2);



}


#endif


实现文件:

#include "mystring.h"
#include <cassert>


namespace main_wtf_5
{

string::string(const char str[])
{
current_length = strlen(str);
alloacted = current_length+1;
characters = new char[alloacted];
strcpy(characters,str);
}


string::string(const string& source)
{
current_length = source.current_length;
alloacted = source.alloacted;
characters = new char[alloacted];
strcpy(characters,source.characters);
}


void string::operator= (const string& source)
{
char* new_str;
if(this == &source)
return;


if(alloacted != source.alloacted)
{
new_str = new char[alloacted];
delete characters;
characters = new_str;
alloacted = source.alloacted;
}
current_length = source.current_length;
strcpy(characters,source.characters);
}


char string::operator[](size_t position) const
{
assert(position <= current_length);
return characters[position];
}


void string::operator+=(const string& addend)
{
// current_length += addend.current_length;
char* new_str = new char[alloacted];
strcpy(new_str,characters);
delete[] characters;
alloacted = current_length + addend.current_length + 1;
characters = new char[alloacted];
copy(new_str,new_str+current_length,characters);
copy(addend.characters,addend.characters+addend.current_length,characters+current_length);
current_length += addend.current_length;
characters[current_length] = '\0';
}


void string::operator+=(const char addend)
{
// reserve(alloacted+1);
char* new_str = new char[alloacted+1];
strcpy(new_str,characters);
new_str[current_length] = addend;
characters = new_str;
alloacted += 1;
current_length += 1;
characters[current_length] = '\0';


}


void string::operator+=(const char addend[])
{
int length = strlen(addend);
char* new_str = new char[alloacted];
strcpy(new_str,characters);
delete[] characters;
alloacted += length;
characters = new char[alloacted];
copy(new_str,new_str+current_length,characters);
copy(addend,addend+length,characters+current_length);
current_length += length;
characters[alloacted] = '\0';
}


void string::reserve(size_t n)
{
char* new_str;


if(alloacted == n)
return;


if(n < current_length)
alloacted = current_length+1;


new_str = new char[n];
strcpy(new_str,characters);
delete[] characters;
characters = new_str;
alloacted = n;
}


string::~string()
{
delete[] characters;
}


string operator +(const string& s1,const string& s2)
{
string str;


str += s1;
str += s2;


return str;
}


istream& operator >>(istream& ins,string& target)
{
while(ins && isspace(ins.peek()))
ins.ignore();
char ch;
target.characters = "";
while(ins>>ch)
{
target += ch;
if(isspace(ins.peek()))
break;
}
target.current_length = strlen(target.characters);
target.alloacted = target.current_length + 1;

return ins;
}


ostream& operator <<(ostream& outs,const string& source)
{
outs<<source.characters;
return outs;
}

bool operator == (const string& s1,const string& s2)
{
return (strcmp(s1.getChar(),s2.getChar()) == 0);
}


bool operator > (const string& s1,const string& s2)
{
return (strcmp(s1.getChar(),s2.getChar()) > 0);
}
bool operator < (const string& s1,const string& s2)
{
return (strcmp(s1.getChar(),s2.getChar()) < 0);


}
bool operator >= (const string& s1,const string& s2)
{
return (strcmp(s1.getChar(),s2.getChar()) >= 0);
}
bool operator <= (const string& s1,const string& s2)
{
return (strcmp(s1.getChar(),s2.getChar()) <= 0);
}
bool operator != (const string& s1,const string& s2)
{
return (strcmp(s1.getChar(),s2.getChar()) != 0);
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值