string类的代码实现

 1 #ifndef __MYSTRING_H__
 2 #define __MYSTRING_H__
 3 #include <iostream>
 4 using namespace std;
 5 class myString
 6 {
 7     friend ostream& operator<<(ostream &out, const myString& s);
 8 public:
 9     myString(int len=0);
10     myString(const char *p);
11     myString(const myString& obj);
12     ~myString();
13 public:
14     myString& operator=(const char *p);
15     myString& operator=(const myString &obj);
16     char& operator[](int index)const;
17 public:
18     bool operator==(const char* p)const;
19     bool operator!=(const char* p)const;
20     bool operator==(const myString& s)const;
21     bool operator!=(const myString& s)const;
22 public:
23     char* c_str();
24     const char* c_str()const;
25     int length();
26 public:
27     int operator<(const char *p);
28     int operator>(const char *p);
29     
30     int operator<(const myString& s);
31     int operator>(const myString& s);
32 
33 private:
34     int m_len;
35     char *m_p;
36 };
37 
38 #endif
  1 #include "mystring.h"
  2 #include <iostream>
  3 #include <stdlib.h>
  4 using namespace std;
  5 
  6 ostream& operator<<(ostream &out, const myString& s)
  7 {
  8     out<<s.m_p;
  9     return out;
 10 }
 11 
 12 myString::myString(int len)
 13 {
 14     m_len=len;
 15     m_p=new char[m_len+1];
 16     memset(m_p,0,sizeof(char)*(m_len+1));
 17     
 18 }
 19 myString::myString(const char *p)
 20 {
 21     int len=strlen(p);    
 22     m_len=len;
 23     m_p=new char[m_len+1];
 24     strcpy(m_p,p);
 25 }
 26 myString::myString(const myString& obj)
 27 {
 28     m_len=obj.m_len;
 29     m_p=new char[m_len+1];
 30     strcpy(m_p,obj.m_p);
 31 }
 32 myString::~myString()
 33 {
 34     if(m_p != NULL)
 35     {
 36         delete []m_p;
 37     }
 38     m_p=NULL;
 39     m_len=0;
 40     
 41 }
 42 
 43 myString& myString::operator=(const char *p)
 44 {
 45     if(m_p != NULL)
 46     {
 47         delete []m_p;
 48         m_len=0;
 49     }
 50     int len=strlen(p);    
 51     m_len=len;
 52     m_p=new char[m_len+1];
 53     strcpy(m_p,p);
 54 }
 55 
 56 myString& myString::operator=(const myString &obj)
 57 {
 58     m_len=obj.m_len;
 59     m_p=new char[m_len+1];
 60     strcpy(m_p,obj.m_p);
 61     return *this;
 62 }
 63 char& myString::operator[](int index)const
 64 {
 65     return m_p[index];
 66 }
 67 
 68 bool myString::operator==(const char* p)const
 69 {
 70     return !strcmp(m_p,p);
 71 }
 72 bool myString::operator!=(const char* p)const
 73 {
 74     return strcmp(m_p,p);
 75 }
 76 bool myString::operator==(const myString& s)const
 77 {
 78     return !strcmp(m_p,s.m_p);
 79 }
 80 bool myString::operator!=(const myString& s)const
 81 {
 82     return strcmp(m_p,s.m_p);
 83 }
 84 
 85 char* myString::c_str()
 86 {
 87     return m_p;
 88 }
 89 const char* myString::c_str()const
 90 {
 91     return m_p;
 92 }
 93 int myString::length()
 94 {
 95     return m_len;
 96 }
 97 
 98 int myString::operator<(const char *p)
 99 {
100     return (strcmp(m_p,p) < 0);    
101 }
102 int myString::operator>(const char* p)
103 {
104     return (strcmp(m_p,p) > 0);
105 }
106 int myString::operator<(const myString &obj)
107 {
108     return strcmp(m_p,obj.m_p) < 0;
109 }
110 
111 int myString::operator>(const myString &obj)
112 {
113     return strcmp(m_p, obj.m_p) > 0;
114 }
1 #include "mystring.h"
2 int main()
3 {
4 
5     myString s("helloworld");
6     s[0]='l';
7     cout<<s.c_str()<<endl;
8     return 0;
9 }

 

posted on 2016-05-05 15:46  川洋  阅读(290)  评论(0)    收藏  举报