数据结构与程序设计(17)String Operator overload_第1页
数据结构与程序设计(17)String Operator overload_第2页
数据结构与程序设计(17)String Operator overload_第3页
数据结构与程序设计(17)String Operator overload_第4页
数据结构与程序设计(17)String Operator overload_第5页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、7/18/2022数据结构与程序设计 1课程内容安排C String 与 String的比较。讨论C+中String Class的实现方法。7/18/2022数据结构与程序设计 2C String 与 String Classstring class: string class in the standard library accessed by #include C strings: can be accessed by #include A String constant is stored as a char array. 即一个常量的string被认为是cstring e.g.: “

2、Hello everyone” / cstring string s = “Hello everyone” /String object 7/18/2022数据结构与程序设计 3What is a C String? Char arrayA C string is a char array terminated by the null character 0 ( with ASCII value 0 ).A C string variable can be initialized in its declaration in following ways.char message 8 = H,

3、e, l, l, o, 0 ;char message 8 = “Hello” ;Char *message = “hello”;message 0 1 2 3 4 5 6 7H e l l o 07/18/2022数据结构与程序设计 4字符串 (String)字符串是n ( 0 ) 个字符的有限序列,记作 S : “c1c2c3cn”其中,S 是串名字 “c1c2c3cn” 是串值 ci 是串中字符 n 是串的长度。 7/18/2022数据结构与程序设计 5functions that manipulate C-strings/字符串的拷贝操作。char *strcpy(char *to,

4、char *from);Pre: The string from has been initialized.Post: The function copies string from to string to, including 0; it returns a pointer to the beginning of the string to./将from指向的内容链接在to的内容之后char *strcat(char *to, char *from);Pre: The strings from and to have been initialized.Post: The function

5、copies string from to the end of string to,including 0; it returns a pointer to the beginning of the string to.7/18/2022数据结构与程序设计 6functions that manipulate C-strings/求字符串的长度int strlen(char *s);Pre: The string s has been initialized.Post: The function returns the length of the string s, not includin

6、g the null byte 0 at the end of the string s./字符串的比较操作。int strcmp(char *s1, char *s2);Pre: The strings s1 and s2 have been initialized.Post: The function compares string s1 to string s2; it returns 0 if s1 0 if s1 s2.7/18/2022数据结构与程序设计 7functions that manipulate C-strings/返回s2第一次在s1中出现的位置。char *strs

7、tr(char *s1, char *s2);Pre: The strings s1 and s2 have been initialized.Post: The function returns a pointer to the first occurrence of the string s2 in the string s1, or it returns NULL if the string s2 is not present in s1.7/18/2022数据结构与程序设计 8Strings in Cchar * p=hello world; /cant be writtenchar

8、* q=new charstrlen(p)+1;strcpy(q,p);coutqendl;/q=p;p=q;*p=H; coutqendl;coutpendl;7/18/2022数据结构与程序设计 9Strings in Cchar *strcpy(char *to, char *from) int i; for(i=0;fromi!=0;i+)toi=fromi; toi=0; return to;7/18/2022数据结构与程序设计 10Strings in Cint strlen(char * s) int i; for(i=0;si!=0;i+); return (i);7/18/2

9、022数据结构与程序设计 11ComparisonBOOK P234 Figure 6.5“ Important string”“ acquires an alias”S2 = s1S2S17/18/2022数据结构与程序设计 12ComparisonC-strings are widely available.C-strings are very efficient.C-string objects are not encapsulated.C-strings are easy to misuse, with consequences that can be disastrous.It is

10、 easy for a client to create either garbage or aliases for C-string data.7/18/2022数据结构与程序设计 13/ * / PrintName program/ This program prints a name in two different formats/ *#include / for cout and endl#include / for data type stringusing namespace std;const string FIRST = “Herman”; / Persons first n

11、ameconst string LAST = “Smith”; / Persons last nameconst char MIDDLE = G; / Persons middle initialC+ Program-STL STRING7/18/2022数据结构与程序设计 14C+ Code Continuedint main( ) string firstLast; / Name in first-last format string lastFirst; / Name in last-first format firstLast = FIRST + “ “ + LAST ; cout “

12、Name in first-last format is “ endl firstLast endl; lastFirst = LAST + “, “ + FIRST + ; cout “Name in first-last format is “ endl lastFirst MIDDLE . endl; return 0; 7/18/2022数据结构与程序设计 15Output of Program Name in first-last format is Herman Smith Name in last-first-initial format is Smith, Herman G.7

13、/18/2022数据结构与程序设计 16Implementation of String Classclass String public: / methods of the string ADTString( );String( );String (const String ©); / copy constructorString (const char * copy); / conversion from C-stringString (List ©); / conversion from Listvoid operator = (const String ©);c

14、onst char *c_str( ) const; / conversion to C-style stringprotected:char *entries;int length;7/18/2022数据结构与程序设计 17运算符重载,全局方法bool operator = (const String &rst, const String &second);bool operator (const String &rst, const String &second);bool operator = (const String &rst, const String &second);bool

15、operator = (const String &rst, const String &second);bool operator != (const String &rst, const String &second);7/18/2022数据结构与程序设计 18String constructorString : String ()entries = new char1;entries0 = 0;length = 0;7/18/2022数据结构与程序设计 19String constructorString : String (List &in_list)/* Post: The Stri

16、ng is initialized by the character List in_list . */length = in_list.size( );entries = new charlength + 1;for (int i = 0; i length; i+) in_list.retrieve(i, entriesi);entrieslength = 0;7/18/2022数据结构与程序设计 20Implementation of String Classconst char*String : c_str( ) const/* Post: A pointer to a legal C

17、-string object matching the String is returned. */return (const char *) entries;7/18/2022数据结构与程序设计 21全局函数的实现Operator Overloadbool operator = (const String &first, const String &second)/* Post: Return true if the String first agrees with String second . Else: Return false . */return strcmp(first.c_st

18、r( ), second.c_str( ) = 0;7/18/2022数据结构与程序设计 22Function Overloadvoid strcat(String &add_to, const String &add_on)/* Post: The function concatenates String add_on onto the end of String add_to .*/const char *cfirst = add_to.c_str( );const char *csecond = add_on.c_str( );char *copy = new charstrlen(cf

19、irst) + strlen(csecond) + 1;strcpy(copy, cfirst);strcat(copy, csecond);add_to = copy; /?delete copy;7/18/2022数据结构与程序设计 23Main ProgramString read_in(istream &input)/* Post: Return a String read (as characters terminated by a new line or an end-of-file character) from an istream parameter. */List temp

20、;int size = 0;char c;while (c = input.peek( ) != EOF & (c = input.get( ) != n)temp.insert(size+, c);String answer(temp);return answer;7/18/2022数据结构与程序设计 24Main Programvoid write(String &s)/* Post: The String parameters is written to cout . */cout s.c_str( ) endl;注意:Char *p = “hello”;Coutp;/将输出字符串hel

21、lo的值。7/18/2022数据结构与程序设计 25Main Programostream & operator (ostream & output, String s1)output s1.c_str( ) endl;return output;7/18/2022数据结构与程序设计 26Main Programvoid main() String s1=s1 string;couts1.c_str()endl;coutstrlen(s1.c_str()endl;String s2(s2 string);couts2.c_str()endl;coutstrlen(s2.c_str()=s2)

22、cout=s2endl;else couts1s2endl;s2=s1;if(s1=s2) couts1=s2endl;else couts1!=s2endl;7/18/2022数据结构与程序设计 29Results1s2s1=s27/18/2022数据结构与程序设计 30Main ProgramString s3=read_in(cin);strcat(s3,s1);write(s3);7/18/2022数据结构与程序设计 31Resultheadheads1 string7/18/2022数据结构与程序设计 32Implementation of String Class目录String下例

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论