此版本较1.0版做了如下改动:
1. 修正了前版本reverse函数实现的一个重要逻辑bug。
2. 用自己实现的My_strlen()函数取代了Cstring库中的strlen()函数。
3. 增加了getline()函数用于取含空格的字符串。
//FILE: mystring.h(part of the namespace leonardo_1)
//CLASS PROVIDED:string (a class to deal issues of string)
#ifndef LEONARDO_1_MYSTRING_H
#define LEONARDO_1_MYSTRING_H
#include<cstdlib> //Provide size_t
#include<iostream>//Provide iostream
using namespace std;
namespace leonardo_1
{
class string
{
public:
//CONSTRUCTOR and DESTRUCTOR for the string class
string(const char str[] = "");
~string();
//CONSTANT MEMBER FUNCTIONS for the string class
size_t length() const;
char operator [](size_t position) const;
//FRIEND CONSTANT MEMBER FUNCTIONS for the string class
friend bool operator==(const string& s1,const string& s2);
//MODIFICATION MEMBER FUNCTION for the string class
void operator +=(const string& addend);
void operator +=(const char addend[]);
void operator +=(char addend);
void operator =(const string& source);
void reserve(size_t n);
private:
char *characters;
std::size_t allocated;
std::size_t current_length;
};
//NONMEMBER FUNCTIONS for the string class:
string operator + (const string& s1, const string& s2);
istream& operator>>(istream& ins,string& target);
istream& getline(istream& ,string& ,char );
ostream& operator<<(ostream& outs,string& target);
size_t My_strlen(const char s[]);
}
#endif
//FILE: mystring.cpp (part of namespace leonardo_1)
//CLASS implemented: string (see mystring.h for documentation)
//INVARIANT for the string class:
// 1. The string use null as its end, it is saved in the dynamic array
// which the characters point to.
// 2. The total length of the dynamic array was saved in the memeber
// variable allocated
// 3. The string in front of the space character is saved in the member
// variable current_length , it is always samller than allocted.
#include<algorithm>
#include<cctype>
#include<cassert>
#include<cstring>
#include<iostream>
#include<stdio.h>
#include"mystring.h"
using namespace std;
namespace leonardo_1
{
//CONSTRUCTOR and DESTRUCTOR for the string class:
string::string(const char str[])
{
current_length=My_strlen(str);
allocated=current_length+1;
characters=new char[allocated];
strcpy(characters, str);
}
string::~string()
{
delete [] characters;
}
//CONSTANT MEMBER FUNCTION for the string class:
size_t string::length() const
{
return current_length;
}
char string::operator [](size_t position) const
{
if(position>=length()+1)
cout<<"Your position isn't existed"<<endl;
else
return characters[position];
}
bool operator==(const string& s1, const string& s2)
{
return (strcmp(s1.characters, s2.characters)==0);
}
//MODIFICATION MEMBER FUNCTIONS for the string class:
void string::operator +=(const string& addend)
{
if(length()+addend.length()<allocated)
{
current_length+=addend.length();
strcat(characters,addend.characters);
}
else
{
reserve(length()+addend.length());
current_length+=addend.length();
strcat(characters,addend.characters);
}
}
void string::operator +=(const char addend[])
{
if(length()+My_strlen(addend)<allocated)
{
current_length+=My_strlen(addend);
strcat(characters,addend);
}
else
{
reserve(length()+My_strlen(addend));
current_length+=My_strlen(addend);
strcat(characters,addend);
}
}
void string::operator +=(char addend)
{
char c[2];
c[0]=addend;
c[1]='/0';
if(length()+1<allocated)
{
current_length+=1;
strcat(characters,c);
}
else
{
reserve(length()+1);
current_length+=1;
strcat(characters,c);
}
}
void string::operator =(const string& source)
{
if(source.length()+1<allocated)
{
current_length=source.length();
strcpy(characters,source.characters);
}
else
{
reserve(source.length());
current_length=source.length();
strcpy(characters,source.characters);
}
}
void string::reserve(size_t n)
{
char *old_characters;
if(n==length())
return;
else if(n<length())
{
old_characters=new char[n+1];
copy(characters,characters+n+1,old_characters);
old_characters[n]='/0';
delete [] characters;
characters=old_characters;
}
else
{
old_characters=new char[n+1];
copy(characters,characters+length()+1,old_characters);
delete [] characters;
characters=old_characters;
}
}
//NONMEMBER MEMBER FUNCTIONS for the string class:
string operator + (const string& s1, const string& s2)
{
string s;
s+=s1;
s+=s2;
return s;
}
istream& operator>>(istream& ins, string& target)
{
char temp;
while(ins && isspace(ins.peek()))
ins.ignore();
target="";
ins.get(temp); //C++风格的cin.get()
while(temp!=' '&&temp!='/n')
{
target+=temp;
ins.get(temp);
}
return ins;
}
istream& getline(istream& ins, string& target,char delimiter='/n')
{
char temp;
target="";
while((temp=getchar())!=delimiter) //使用C语言风格的getchar()
{
target+=temp;
}
return ins;
}
ostream& operator<<(ostream& outs,string& target)
{
size_t i=0;
while(target[i]!='/0')
{
cout<<target[i];
i++;
}
return outs;
}
size_t My_strlen(const char s[])
{
size_t i=0;
while(s[i]!='/0')
i++;
return i;
}
}