今天帮着一位大二的学弟写了一个String的类,后来一想这个技术点,也许不是什么难点,但是还是简单的记录一些吧! 为那些还在路上爬行的行者,剖析一些基本的实现.....
内容写的过于简单,没有涉及到其他格式的如考虑utf_8.这儿单纯的考虑了char的使用.......
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
using namespace std; class MyString { public :
MyString(const char * str = NULL) ; //默认构造函数
MyString(const MyString & mystr); //复制构造函数
~MyString() {
if (test != NULL) delete[] test;
};
//析构函数
int length(); //返回字符串长度
void print(); //打印
char at(int pos) ; //查找第i个字符
MyString & operator = (const MyString &other); //等号操作符重载
MyString & operator = (const char * str ); //等号操作符重载
private:
char * test;
}; //构造函数
MyString::MyString(const char * str) { if (str == NULL)
{
test = new char[];
*test = '\0';
}
else
{
int length = strlen(str);
test = new char[length + ];
strcpy(test, str); //复制
}
} MyString::MyString(const MyString & mystr) //复制构造函数
{
char * pstr = mystr.test;
test = new char [strlen(pstr)+]; //开辟空间
strcpy(test, mystr.test); //复制类容
} //返回长度
int MyString::length() { int i = ;
if (test == NULL) return ;
while (test[i] != '\0') i++;
return i;
} void MyString::print() { if (test != NULL) {
//printf("%s\n",test);
puts(test); //这样可以输出空格
}
} char MyString::at(int pos) { if (test != NULL) {
int len = strlen(test);
if (len <=pos) {
throw out_of_range("位置超过字符串长度!");
}
else {
return test[pos];
}
} {
throw out_of_range("字符串为空!");
}
} MyString & MyString::operator =(const MyString & aa)
{
if (this == &aa)//当地址相同时,直接返回;
return *this;
delete[] test;//当地址不相同时,删除原来申请的空间,重新开始构造;
int length = strlen(aa.test);
test = new char [length + ];
strcpy(test, aa.test);
return *this; } MyString & MyString::operator = (const char * str) //等号操作符重载
{ MyString * ss = new MyString (str);
return *ss;
} int main(){ char *pp = "sadasd";
MyString aa = "abcd";
aa.print(); //显示
cout << "长度为:" << aa.length();
cout << "显示abcd第二个元素的内容"<<aa.at()<<endl; MyString *str = new MyString(pp); //对于指针而言
str->print();
cout << "长度为:" << str->length();
cout << "显示sadasd第二个元素的内容" << str->at()<<endl; MyString bb(aa); //对于复制构造函数而言
bb.print();
cout << "长度为:" << bb.length();
cout << "显示abcd第二个元素的内容" << bb.at()<<endl;
str->print();
getchar();
}
对于这一点,后来又加深了一些基本模式,简略的实现以下String类吧!
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std; template < typename T > class MyString { public : MyString(const T * str = NULL) ;
//默认构造函数
MyString(const MyString<T> & mystr); //复制构造函数
~MyString() {
if (test != NULL) delete[] test;
};
//析构函数
int length(); //返回字符串长度
void print(); //打印
T at(int pos);
MyString<T> & operator + (const MyString<T> &aa); //等号操作符重载
MyString<T> & operator + (const T * str); //等号操作符重载
MyString<T> & operator = (const MyString<T> &aa); //等号操作符重载
MyString<T> & operator = (const T * str ); //等号操作符重载 private:
T * test; }; template < typename T >
MyString<T> & MyString<T>::operator + (const MyString<T> &aa) { T *str =test;
int lena = - , lenb=-;
lena=strlen(aa.test);
lenb = strlen(test);
if (lena> ) {
test = new T[lena+lenb];
strcpy(test, strcat(str, aa.test)); }
return *this;
} template < typename T >
MyString<T> & MyString<T>::operator + (const T * str) { T * str = test;
int lena = -, lenb = -;
lena = strlen(str);
lenb = strlen(test);
if (lena > ) {
test = new T[lena + lenb];
strncpy(test, strcat(str, str));
}
return *this;
} //构造函数
template < typename T >
MyString<T>::MyString(const T * str) { if (str == NULL)
{
test = new T[];
*test = '\0';
}
else
{
int length = strlen(str);
test = new T[length + ];
strcpy(test, str); //复制
}
} template < typename T >
MyString<T>::MyString(const MyString<T> & mystr) //复制构造函数
{
T * pstr = mystr.test;
test = new T [strlen(pstr)+]; //开辟空间
strcpy(test, mystr.test); //复制类容
} //返回长度
template < typename T >
int MyString<T>::length() { int i = ;
if (test == NULL) return ;
while (test[i] != '\0') i++;
return i;
}
template < typename T >
void MyString<T>::print() { if (test != NULL) {
//printf("%s\n",test);
puts(test); //这样可以输出空格
}
}
template < typename T >
T MyString<T>::at(int pos) { if (test != NULL) {
int len = strlen(test);
if (len <=pos) {
throw out_of_range("位置超过字符串长度!");
}
else {
return test[pos];
}
} {
throw out_of_range("字符串为空!");
}
} template < typename T >
MyString<T> & MyString<T>::operator =(const MyString<T> & aa)
{
if (this == &aa)//当地址相同时,直接返回;
return *this;
delete[] test;//当地址不相同时,删除原来申请的空间,重新开始构造;
int length = strlen(aa.test);
test = new T [length + ];
strcpy(test, aa.test);
return *this; } template < typename T >
MyString<T> & MyString<T>::operator = (const T * str) //等号操作符重载
{ MyString<T> * ss = new MyString<T> (str);
return *ss;
} int main(){ char *pp = "sadasd";
MyString<char> aa = "abcd";
aa.print(); //显示
cout << "长度为:" << aa.length();
cout << "显示abcd第二个元素的内容"<<aa.at()<<endl; MyString<char> *str = new MyString<char>(pp); //对于指针而言
str->print();
cout << "长度为:" << str->length();
cout << "显示sadasd第二个元素的内容" << str->at()<<endl; MyString<char> bb(aa); //对于复制构造函数而言
bb.print();
cout << "长度为:" << bb.length();
cout << "显示abcd第二个元素的内容" << bb.at()<<endl;
str->print();
MyString<char> sc="你好,北京人";
sc.print();
sc = sc + bb;
sc.print();
getchar();
return ;
}
String类的先关补充.....
/*
String类的功能为:
1.构造函数
2.重载赋值操作符
3.重载下标操作符
4.重载关系操作符
5.重载转换操作符
6.析构函数
*/
#include<iostream>
#include<string.h>
#include<assert.h>
#include<string>
using namespace std; class String { //重载输出操作符
friend ostream & operator << (ostream &out, String & str);
friend istream & operator >> (istream &in, String & str);
//重载关系操作符
friend bool operator == (const String &src , const String &op);
friend bool operator != (const String &src, const String &op);
friend bool operator < (const String &src, const String &op);
public :
//定义构造函数
String(); //default
String(const char * str); //elements
String(const String &str); //copy
//重载赋值操作符
String & operator =(const String & str);
String & operator =(const char * str);
String & operator =(const char str);
//+=重载运算操作符
String & operator +=(const String & str);
String & operator +=(const char * str);
String & operator +=(const char str); //重载[]操作符
char & operator [] (int index );
//重载转换操作符
operator char*();
~String();
private:
char *gg ; //字符指针
}; ostream & operator << (ostream &out, String & str) {
out << str.gg ;
return out;
} istream & operator >> (istream &in, String & str)
{
in >> str.gg;
return in ;
} bool operator == (const String &src, const String &op) {
return strcmp(src.gg , op.gg) == ;
} bool operator != (const String &src, const String &op)
{
return strcmp(src.gg , op.gg )!=;
} bool operator < (const String &src, const String &op) {
return strcmp(src.gg ,op.gg )<;
} String::String() {
gg = new char[];
gg = '\0';
}; String::String(const char * str) {
int var = strlen(str);
gg = new char[var+];
strcpy(gg ,str);
} String::String(const String &str) {
int var = strlen(str.gg);
gg = new char[var + ];
strcpy(gg, str.gg);
} String & String::operator =(const String & str) {
if (this == &str) return *this;
delete gg;
int var = strlen(str.gg);
gg = new char[var + ];
strcpy(gg, str.gg);
gg[var] = '\0';
return *this;
} char & String::operator [] (int index) {
assert(index>=&&index<strlen(gg));
return gg[index];
} String & String::operator =(const char * str) {
if (strcmp(gg,str)==) return *this;
int var = strlen(str);
gg = new char(var + );
strcpy(gg, str);
gg[var] = '\0';
return *this;
} String & String::operator = (const char str) { gg = new char();
gg[] = str;
gg[] = '\0';
return *this;
} String::operator char *() {
return gg;
} String::~String() {
delete gg;
} String & String::operator +=(const String & str) {
int var = strlen(str.gg);
if (gg == NULL)
{
gg = new char(var+);
strcpy(gg,str.gg);
gg[var] = '\0';
}
else {
char *p = gg;
int vat = strlen(p) + var;
gg = new char(vat+var);
strcpy(gg,p);
delete p;
strcat(gg,str.gg);
gg[vat] = '\0';
}
return *this;
} String & String::operator +=(const char * str) { int var = strlen(str);
if (gg == NULL)
{
gg = new char(var + );
strcpy(gg, str);
gg[var] = '\0';
}
else {
char *p = gg;
int vat = strlen(p) + var;
gg = new char(vat + var);
strcpy(gg, p);
delete p;
strcat(gg, str);
gg[vat] = '\0';
}
return *this; }
String & String::operator +=(const char str) { if (gg == NULL)
{
gg = new char();
gg[] == str;
gg[] = '\0';
}
else {
char *p = gg;
int vat = strlen(p)+;
gg = new char(vat + );
strcpy(gg, p);
delete p;
gg[vat - ] = str;
gg[vat] = '\0';
}
return *this;
}