MyString(重写String)

http://wenku.baidu.com/view/d7ac113243323968011c925b.html

http://blog.csdn.net/wxm349810930/article/details/52553578

已知类String的原型为:

class String  {

public:    String(const char *str = NULL); // 普通构造函数

String(const String &other);     // 拷贝构造函数

~ String(void);         // 析构函数

String & operate =(const String &other); // 赋值函数

private:    char   *m_data;    // 用于保存字符串

};

请编写String的上述4个函数。

  1 /*
  2 ccnu_hupo_cpp_class_tst6exercise2_by:lele_2013_10_30
  3    new不忘delete
  4 Design the string class in the C++ library by providing your own implementation for the following functions (name your class MyString):
  5
  6 MyString();//构造
  7 MyString(const char* cString);//地址不能变
  8 char at(int index) const;//输入数组下标,返回字符
  9 int length() const;//长度
 10 void clear();//清空 len=0
 11 bool empty() const;//是否清空?len不变
 12 int compare(const MyString& s) const;//
 13 int compare(int index, int n, const MyString& s) const;
 14 void copy(char s[], int index, int n);
 15 char* data() const;
 16 int find(char ch) const;
 17 int find(char ch, int index) const;
 18 int find(const MyString& s, int index) const;
 19
 20 */
 21
 22 #include<iostream>
 23 #include<string.h>
 24 using namespace std;
 25 class MyString
 26 {
 27 public:
 28     MyString();
 29     MyString(const char* cString);//ordinary func
 30     MyString(const MyString &other);//copy func
 31     MyString &operator =(const MyString &other);//assign func
 32     //~MyString(void);
 33     char at(int index) const;//
 34     int length() const;
 35     void clear();
 36     bool empty() const;
 37     int compare(const MyString& s) const;
 38     int compare(int index, int n, const MyString& s) const;
 39     void copy(char s[], int index, int n);
 40     char* data() const;
 41     int find(char ch) const;
 42     int find(char ch, int index) const;
 43     int find(const MyString& s, int index) const;
 44
 45     MyString operator + (const MyString & another);
 46
 47     bool operator > (const MyString & another);
 48     bool operator < (const MyString & another);
 49     bool operator == (const MyString & another);
 50
 51     char& operator[](int idx);
 52
 53     void dis();
 54
 55     ~MyString()
 56     {
 57         cout<<"delete..."<<endl;
 58         delete []a;
 59     }
 60
 61 private:
 62     char *a;
 63     int len;
 64 };
 65
 66 MyString::MyString(){
 67     cout << "MyString()" << endl;
 68 }
 69
 70 MyString::MyString(const char* cString)
 71 {
 72     if (cString==NULL)
 73     {
 74         a=new char[1];
 75         a[0]=0;
 76         //int len=0;
 77     }
 78     else
 79     {
 80         len = strlen(cString);//new
 81         a=new char [len+1];
 82         strcpy(a,cString);
 83         //len=strlen(cString);
 84     }
 85 }
 86
 87 MyString::MyString(const MyString &other){
 88     int len = strlen(other.a);
 89     a = new char[len+1];
 90     strcpy(a, other.a);
 91 }
 92
 93 MyString &MyString::operator =(const MyString &other){
 94     //self check assign
 95     if(this == &other){
 96         return *this;
 97     }
 98     //释放原有的内存资源
 99     delete []a;
100     //分配新的内存资源,并复制内容
101     int len = strlen(other.a);
102     a = new char[len+1];
103     strcpy(a, other.a);
104     //返回本对象的引用
105     return *this;
106 }
107
108 char MyString::at(int index) const
109 {
110     if(len==0)
111     {cout<<"no char in string"<<endl;
112         return 0;
113     }
114     if(index>len)
115     {
116         cout<<"the maximun array exceed"<<endl;
117         return 0 ;
118     }
119     else
120     {
121
122         return a[index-1];
123     }
124 }
125
126 int MyString::length() const
127 {
128     return len;
129 }
130
131 void MyString::clear()
132 {
133     if(!a)
134     {
135         delete []a;
136         a=NULL;
137     }
138
139     len=0;
140     //a=" ";
141 }
142 bool MyString::empty() const
143 {
144     if(len==0)
145         return true;
146     else
147         return false;
148 }
149
150
151 int MyString::compare(const MyString& s) const
152 {
153     int m=this->len;int n=s.len;
154
155     for(int i=0;i<m&&i<n;i++)
156     {
157         if(s.a[i]==a[i])
158             continue;
159         else if(s.a[i]<a[i])
160             return 1;
161         else
162             return -1;
163     }
164     return 0;
165
166
167 }
168 int MyString::compare(int index, int n, const MyString& s) const
169 {
170     int m=len,k=s.len;
171     if(index+n>m||index+n>k)
172     {
173         cout<<"cannot compare!"<<endl;
174         ///I dont know how to solve it
175     }
176     for(int i=index-1,j=0;i<n+index;i++,j++)
177     {
178         if(s.a[j]==a[i])
179             continue;
180         else if(s.a[j]<a[i])
181             return 1;
182         else
183             return -1;
184     }
185     return 0;
186 }
187
188
189 void MyString::copy(char s[], int index, int n)
190 {
191     if(n>=len)
192     {cout<<"the maximun array exceed"<<endl;}
193     else  if(n+index-1>len)
194     {
195         cout<<"the maximun array exceed"<<endl;
196         return;
197     }
198     else
199     {
200         for(int i=n-1,j=0;i<n+index-1;i++,j++)
201             s[j]=a[i];
202     }
203
204 }
205 char* MyString::data() const
206 {
207     return a;
208 }
209
210
211
212
213 int MyString::find(char ch) const
214 {
215
216     for(int i=0;i<len;i++)
217     {
218         if(ch==a[i])
219             return i+1;
220     }
221
222     return 0;
223 }
224 int MyString::find(char ch, int index) const
225 {
226     if(index>len||index<1)
227     {
228         cout<<"the index num is should be  1~ "<<len<<endl;
229         return 0;
230     }
231     for(int i=index-1;i<len;i++)
232     {
233         if(ch==a[i])
234             return i+1;
235     }
236     return -1;
237
238 }
239
240 int MyString::find(const MyString& s, int index) const
241 {
242     if(index>s.len||index<1)
243     {
244         cout<<"the index num is should be  1~ "<<s.len<<endl;
245         return 0;
246     }
247     char ch=s.a[index-1];
248
249     for(int i=0;i<len;i++)
250     {
251         if(ch==a[i])
252             return i+1;
253     }
254
255     return 0;
256 }
257
258 // 加法运算符重载
259 MyString MyString::operator +(const MyString & another){
260     int len = strlen(this->a) + strlen(another.a);
261     MyString str;
262     delete []str.a;
263     str.a = new char[len + 1];
264     memset(str.a,0,len + 1);
265     //int len1 = strlen(this->a) + 1;
266     //strcat_s(str.a, len1, this->a);
267     strcat(str.a, this->a);
268     // 源串长度 + 目标串长度 + 结束符
269     //int len2 = strlen(this->a) + strlen(another.a) + 1;
270     //strcat_s(str.a,len2, another.a);
271     strcat(str.a, another.a);
272     return str;
273 }
274
275 // ==关系运算符重载
276 bool MyString::operator==(const MyString &other)
277 {
278     if(strcmp(this->a,other.a) == 0)
279         return true;
280     else
281         return false;
282 }
283 // >关系运算符重载
284 bool MyString::operator>(const MyString &other)
285 {
286     if(strcmp(this->a,other.a) > 0)
287         return true;
288     else
289         return false;
290 }
291 // <运算符重载
292 bool MyString::operator<(const MyString &other)
293 {
294     if(strcmp(this->a,other.a) < 0)
295         return true;
296     else
297         return false;
298 }
299 // []运算符重载
300 char& MyString::operator[](int idx)
301 {
302     return a[idx];
303 }
304
305 int main(){
306     MyString ms;
307     ms = "string";
308     cout << ms[0] << ms[1] << ms[2] << endl;
309     return 0;
310 }

时间: 2024-08-21 04:51:40

MyString(重写String)的相关文章

重写String类

主要是4个默认函数的重写: 代码: #include <iostream> using namespace std; class Cstring{ private : char * data; public : Cstring(const char * str =NULL); Cstring(const Cstring &another); ~Cstring(); Cstring & operator=(const Cstring &another); }; //赋值构

重写String类,也有些区别,供参考

头文件如下: #pragma once #include <string> #include <string.h> #include <stdlib.h> #include <stdio.h> #include <assert.h> using namespace std; typedef int BOOL; #define FALSE 0 #define TRUE 1 typedef unsigned char BYTE; typedef un

C++ string 类重写

(我们知道学习C++时,在学习完C的基础内容后最先上手的就是C++的string类来学习字符串处理的内容,这里我们通过重写string类来重新认识字符串处理的内容) 1.树立string类主要函数,确立我们要实现的内容 2.代码实现 3.测试 参考: 1.http://blog.csdn.net/wxm349810930/article/details/52553578

在java中string为什么要设计成final的

各种不同的理解 1 将方法或类声明为final主要目的是:确保它们不会再子类中改变语义.String类是final类,这意味着不允许任何人定义String的子类.换言之,如果有一个String的引用,它引用的一定是一个String对象,而不可能是其他类的对象.——<Java核心技术 卷I> 2 举个例子:一个方法可能本来接受String类型并返回其大写方式 public static String uppperString(String s){ return s.toUpperCase();

(1)Object类 (2)包装类和数学处理类 (3)String类

1.Object类1.1 基本概念 java.lang.Object类是Java类层次结构的根类,任何类都是Object类的直接/间接子类. 1.2 常用的方法(重点) Object() - 无参构造方法 boolean equals(Object obj) - 用于判断调用对象是否和参数对象相等. - 该方法默认比较两个对象的地址,与 == 运算符的效果等价. - 当需要比较对象的内容时则重写该方法,重写该方法后记得重写hashCode()方法. int hashCode() - 用于获取调用

java内存分配和String类型的深度解析(转)

一.引题 在java语言的所有数据类型中,String类型是比较特殊的一种类型,同时也是面试的时候经常被问到的一个知识点,本文结合java内存分配深度分析 关于String的许多令人迷惑的问题.下面是本文将要涉及到的一些问题,如果读者对这些问题都了如指掌,则可忽略此文. 1.java内存具体指哪块内存?这块内存区域为什么要进行划分?是如何划分的?划分之后每块区域的作用是什么?如何设置各个区域的大小? 2.String类型在执行连接操作时,效率为什么会比StringBuffer或者StringBu

【转】java内存分配和String类型的深度解析

一.引题 在java语言的所有数据类型中,String类型是比较特殊的一种类型,同时也是面试的时候经常被问到的一个知识点,本文结合java内存分配深度分析关于String的许多令人迷惑的问题.下面是本文将要涉及到的一些问题,如果读者对这些问题都了如指掌,则可忽略此文. 1.java内存具体指哪块内存?这块内存区域为什么要进行划分?是如何划分的?划分之后每块区域的作用是什么?如何设置各个区域的大小? 2.String类型在执行连接操作时,效率为什么会比StringBuffer或者StringBui

Java内存分配和String类型的深度解析

一.引题 在java语言的所有数据类型中,String类型是比较特殊的一种类型,同时也是面试的时候经常被问到的一个知识点,本文结合java内存分配深度分析关于String的许多令人迷惑的问题.下面是本文将要涉及到的一些问题,如果读者对这些问题都了如指掌,则可忽略此文. 1.java内存具体指哪块内存?这块内存区域为什么要进行划分?是如何划分的?划分之后每块区域的作用是什么?如何设置各个区域的大小? 2.String类型在执行连接操作时,效率为什么会比StringBuffer或者StringBui

Check if a string is NULL or EMPTY using PowerShell

http://techibee.com/powershell/check-if-a-string-is-null-or-empty-using-powershell/1889 Check if a string is NULL or EMPTY using PowerShellby TECHIBEE on OCTOBER 10, 2012 In this post, I will show you how to verify if a string is empty, null or havin