自己的string类(有测试,C++,QT环境下,与其他环境区别不大):
由于许多网络博客有错误地方,自己试着写完string类的实现之后,想拿出来与大家分享,欢迎朋友们指正,评论和修改!!!一起进步,这个实现方法中存在的函数已经测试过,有可能提升的地方请大家指出!!!
1 \ 头文件(ourstring.h)
#ifndef _ourstring_h
#define _ourstring_h
#include <iostream>
#include <cstring>
#include <cassert>
using namespace std;
static const int MAX_LENGTH = 256;
class OurString
{
friend OurString operator+(OurString& str1, OurString& str2);
friend void operator+=(OurString& str1, OurString& str2);
friend ostream& operator<<(ostream& out,
const OurString& output);
friend istream& operator>>(istream& in, OurString& input);
public:
//constuctors and destructors;
OurString();
OurString(const char *str);
OurString(const OurString& dstr);
//拷贝构造函数,必须是引用传参,不然来回调用了(传参也是拷贝过程);
~OurString();
//convert it to string;
string toString();
//get a substring;
OurString substr(int start, int n);
OurString substr(int start);
//getlength;
unsigned int length() const;
//redifinitions;
OurString& operator=(const OurString& str);
bool operator==(const OurString& str);
bool operator!=(const OurString& str);
bool operator>=(const OurString& str);
bool operator<=(const OurString& str);
bool operator>(const OurString& str);
bool operator<(const OurString& str);
char operator[](int index) const;
private:
bool isinbound(int index) const;
char *ptr = NULL;
unsigned int len;
};
OurString operator+(OurString& str1, OurString& str2);
void operator+=(OurString& str1, OurString& str2);
ostream& operator<<(ostream& out, const OurString& output);
istream& operator>>(istream& in, const OurString& input);
#endif
2 \ 实现文件(ourstring.cpp)
#include "ourstring.h"
#include "strlib.h"
using namespace std;
//----------------FRIEND-----------------//
OurString operator+(OurString& str1, OurString& str2)
{
if(str1.len + str2.len > MAX_LENGTH)
{
cout << "Sorry, the string is too large! We can't add them!";
return str1;
}
OurString _Added = str1;
_Added.ptr = strcat(_Added.ptr, str2.ptr);
_Added.len = str1.len + str2.len;
return _Added;
}
void operator+=(OurString& str1, OurString& str2)
{
if(str1.len + str2.len > MAX_LENGTH)
cout << "Sorry, the string is too large! We can't add them!";
strcat(str1.ptr, str2.ptr);
str1.len = str1.len + str2.len;
}
ostream& operator<<(ostream& out, const OurString& output)
{
out << output.ptr;
return out;
}
istream& operator>>(istream& in, OurString& input)
{
in >> input.ptr;
input.len = strlen(input.ptr);
return in;
//应有一个清空函数(if超过了,清空in),暂时没找到;
}
//------------------constrectors and destructors-------------------//
OurString::OurString()
{
ptr = new char[1];
ptr[0] = '\0';
assert(ptr != 0);
len = 0;
}
OurString::OurString(const char *str)
{
if(strlen(str) > MAX_LENGTH)
{
cout << "Sorry, the string is too large!";
return;
}
if(ptr != NULL)
{
delete [] ptr;
}
ptr = new char[strlen(str) + 1];
assert(ptr != 0);
ptr = strcpy(ptr, str);
len = strlen(str);
}
OurString::OurString(const OurString& dstr)
{
if(ptr != NULL)
{
delete [] ptr;
}
ptr = new char[dstr.len + 1];
assert(ptr != 0);
ptr = strcpy(ptr, dstr.ptr);
len = dstr.len;
}
OurString::~OurString()
{
if(ptr != NULL)
delete [] ptr;
}
//-----------------others--------------//
string OurString::toString()
{
string Convertion = ptr;
return Convertion;
}
OurString OurString::substr(int start)
{
OurString substring;
if( !isinbound(start) )
{
cout << "It is out of the bound!!!";
return substr(len, 1);
}
substring.ptr = new char[len + 1 - start];
for(int i = 0; i <= len - start; i++)
{
substring.ptr[i] = ptr[start + i];
}
substring.len = len - start;
return substring;
}
OurString OurString::substr(int start, int n)
{
if( !isinbound(start + n - 1) )
{
cout << "It is out of the bound!!!";
return substr(len, 1);
}
else
{
OurString substring;
substring.ptr = new char[n + 1];
int i = 0;
for(; i < n; i++)
substring.ptr[i] = ptr[start + i];
substring.ptr[i] = '\0';
substring.len = n;
return substring;
}
}
unsigned int OurString::length() const
{
return len;
}
bool OurString::operator==(const OurString& str)
{
int IF = strcmp(ptr, str.ptr);
if(IF == 0)
return true;
else
return false;
}
bool OurString::operator!=(const OurString& str)
{
int IF = strcmp(ptr, str.ptr);
if(IF == 0)
return false;
else
return true;
}
bool OurString::operator>(const OurString& str)
{
int IF = strcmp(ptr, str.ptr);
if(IF > 0)
return true;
else
return false;
}
bool OurString::operator>=(const OurString& str)
{
int IF = strcmp(ptr, str.ptr);
if(IF >= 0)
return true;
else
return false;
}
bool OurString::operator<(const OurString& str)
{
int IF = strcmp(ptr, str.ptr);
if(IF < 0)
return true;
else
return false;
}
bool OurString::operator<=(const OurString& str)
{
int IF = strcmp(ptr, str.ptr);
if(IF <= 0)
return true;
else
return false;
}
char OurString::operator[](int index) const
{
if(!isinbound(index))
{
cout << "The index is not in bound of the string!!!";
return '\0';
}
return ptr[index];
}
OurString& OurString::operator=(const OurString& str)
{
if (this == &str) return *this;
if(ptr != NULL)
{
delete [] ptr;
}
ptr = new char[str.len + 1];
assert(ptr != 0);
ptr = strcpy(ptr, str.ptr);
len = str.len;
return *this;
}
bool OurString::isinbound(int index) const
{
if(index <= len && index >= 0)
return true;
else
return false;
}
3 \ 主函数(测试)
#include <iostream>
#include <cstring>
#include "console.h"
#include "ourstring.h"
using namespace std;
/*
* Main program
*/
int main()
{
cout<<"Let's test OurString!"<< endl;
OurString s0;
OurString s1("qwe");
OurString s2 = "123456";
OurString s3;
s3 = s1 + s2;
cout << "s0 = " << s0 << "yes!" << endl;
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;
cout << (s1 == s2) << " " << (s1 < s2) << " "
<< (s1 > s2) << " " << (s1 <= s2) << " "
<< (s1 >= s2) << " " << (s1 != s2) << endl;
cout << "s3 = " << s3 << endl;
s3 += s2;
cout << "s3 = " << s3 << endl;
OurString s4;
cout << "Please input s4: ";
cin >> s4;
cout << "s4 = " << s4 << endl;
string str = s4.toString();
cout << "String of s4 = " << s4 << endl;
cout << s4.substr(4, 5) << endl;
cout << s4.substr(4) << endl;
cout << s4.length() << endl;
cout << s4[5] << endl;
s4 = s1;
cout << s4 << endl;
return 0;
}
这是“in the bound”的情况。
这是“out of bound”的情况。