闻心阁

一蓑烟雨看苍生,半壶浊酒笑红尘

自己写了个String类

2010-09-09 约 2 分钟读完 搬砖秘籍

// String.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <cstring>
using namespace std;

//=============String类的定义=============
class String
{
public:
String();             //默认构造函数
String(char *str);    //构造函数
String(String &str1); //拷贝构造函数
    ~String();            //析构函数

int length();         //length()函数
    int find(String &str2); //find函数

//=============运算符重载==================
String operator + (String &t_s1);
void operator = (String &t_s2);
friend ostream &operator << (ostream &os,String &t_s3);
friend istream &operator >> (istream &os,String &t_s4);
friend bool operator < (String &s1,String & s2);
friend bool operator > (String &s1,String & s2);
char operator [] (int i);

private:
    int m_len;
char   *m_str;
};

//=============string类的实现=============
String::String() //默认构造函数
{
m_str=new char[1];
m_str[0]='\0';
m_len=0;
}

String::String(char *str) //构造函数
{
m_len=(int)strlen(str);
m_str=new char[m_len+1];
for (int i=0;i<m_len;i++)
{
    m_str[i]=str[i];
}
m_str[m_len]='\0';

}

String::String(String &str1) //拷贝构造函数
{
m_len=str1.m_len;
m_str=new char[m_len+1];
for (int i=0;i<m_len;i++)
   {
    m_str[i]=str1.m_str[i];
   }
m_str[m_len]='\0';

}

String::~String() //析构函数
{
    m_len=0;
delete []m_str;
}

int String::length() //length()函数
{
return m_len;
}

int String::find(String &str2) //find函数
{
//=======Brute-Force算法==============
   int i = 0, j = 0;
   int k=(int)strlen(str2.m_str);
       while (i<m_len && j<k)
       {
     if (m_str[i] == str2.m_str[j])
              {
                     i++;
                     j++;
              }
              else
              {
                     i=i-j+1;
                     j=0;
              }
       }
       if (j>=k)
              return i-k;
       else
              return -1;
}


//==============运算符重载实现============

String String::operator + (String &t_s1)
{
char temp[100];
int t=m_len;
for(int i=0;i<t;i++)
   temp[i]=m_str[i];
temp[m_len]='\0';
m_len=m_len+t_s1.length();
m_str=new char[m_len+1];
for(int j=0;j<t;j++)
   m_str[j]=temp[j];
for(int k=t;k<m_len;k++)
   m_str[k]=t_s1.m_str[k-t];
m_str[m_len]='\0';
return *this;

}

void String::operator =(String &t_s2)
{
m_len=t_s2.m_len;
m_str=new char[m_len+1];
for(int i=0;i<m_len;i++)
   m_str[i]=t_s2.m_str[i];
m_str[m_len]='\0';

}

bool operator < (String &s1,String & s2)
{
return (strcmp(s1.m_str,s2.m_str)<0);
}

bool operator > (String &s1,String & s2)
{
return (strcmp(s1.m_str,s2.m_str)>0);
}

ostream &operator << (ostream &os,String &t_s3)
{
os<<t_s3.m_str;
return os;
}

istream &operator >> (istream &os,String &t_s4)
{
os>>t_s4.m_str;
t_s4.m_len=(int)strlen(t_s4.m_str);
return os;
}

char String::operator [] (int i)
{
    return m_str[i];
}


//=============main主函数=================
int _tmain(int argc, _TCHAR* argv[])
{
String s1,s2,s3,s4("abcdef");
cin>>s1>>s2;
s3=s1+s2;
cout<<s3<<endl;
for(int i=0;i<s3.length();i++)
   cout<<s3[i];
cout<<endl;
//比较两个字符串的大小,按字符顺序
if(s1>s2)
   cout<<s1<<" is more lager than "<<s2<<endl;
//输出s4在s3中位置
cout<<s3.find(s4);
return 0;
}