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);
}
}