天津大学:《数据结构 Data Structures》课程教学资源(PPT课件)第四章 字符串(String)

字串( String 字符串是n(≥0)个字符的有限序列 记作S:“c1c2C3 ●●● 其中,S是串名字 123° cn”是串值 c1是串中字符 n是串的长度。 例如,S=“ Tsinghua University
字符串 (String) 字符串是 n ( 0 ) 个字符的有限序列, 记作 S : “c1 c2 c3…cn ” 其中,S 是串名字 “ c1 c2 c3…cn ”是串值 ci 是串中字符 n 是串的长度。 例如, S = “Tsinghua University

字符串抽象数据类型和类定义 const int maxLen=128: class string 3 int curlen: /的当前长度 char sch 的存储数组 public: String( const String ob ) String( const char x init ) String o; Strigoi delete[ ch; 3
const int maxLen = 128; class String { int curLen; //串的当前长度 char *ch; //串的存储数组 public: String ( const String& ob ); String ( const char * init ); String ( ); ~String ( ) { delete [ ] ch; } 字符串抽象数据类型和类定义

int Length const i return curLen; f /求当前串this的实际长度 String &operator(( int pos, int len ) /取thi从pos开始len个字符组成的子串 int operator =- const String &ob) ireturn strcmp(ch, ob.ch)=0;3 /判当前串*this与对象串ob是否相等 int operator !=( const String &ob const return strcmp(ch, ob.ch)!=0; 3 /判当前串ths与对象串ob是否不等
int Length ( ) const { return curLen; } //求当前串*this的实际长度 String &operator ( ) ( int pos, int len ); //取*this从pos开始len个字符组成的子串 int operator == ( const String &ob ) { return strcmp (ch, ob.ch) == 0; } //判当前串*this与对象串ob是否相等 int operator != ( const String &ob ) const { return strcmp (ch, ob.ch) != 0; } //判当前串*this与对象串ob是否不等

int operator ! o const i return curLen ==0; 5 /判当前串“this是否空串 String &operator =(String &ob); /将串ob赋给当前串*this String &operator +=(String &ob); /将串ob连接到当前串“ths之后 char &operator [ int 1); /取当前串“th的第i个字符 int Find( string &e pat ) const;
int operator ! ( ) const { return curLen == 0; } //判当前串*this是否空串 String &operator = (String &ob); //将串ob赋给当前串*this String &operator += (String &ob); //将串ob连接到当前串*this之后 char &operator [ ] ( int i ); //取当前串*this的第 i 个字符 int Find ( String& pat ) const; }

字符串部分操作的实现 String String( const String ob)t 复制构造函数:从已有串ob复制 ch= new charImaxLen+11;创刨建串数组 if ch==NULL cer<<“存储分配错!Ⅷm”; exit(1); curlen= ob. curlen;1复制串长度 Cpy(ch,obch);/复制串值 sti
String :: String ( const String& ob ) { //复制构造函数:从已有串ob复制 ch = new char[maxLen+1]; //创建串数组 if ( ch == NULL ) { cerr << “存储分配错! \n”; exit(1); } curLen = ob.curLen; //复制串长度 strcpy ( ch, ob.ch ); //复制串值 } 字符串部分操作的实现

String String ( const char init )i 复制构造函数:从已有字符数组init复制 ch= new char maxLen+1];//创建串数组 if(ch== NULL cer<<“存储分配错!Ⅷm”; exit(D) curlen= strlen(init);//复制串长度 strcpy(cl , Init ); //复制串值
String :: String ( const char *init ) { //复制构造函数: 从已有字符数组*init复制 ch = new char[maxLen+1]; //创建串数组 if ( ch == NULL ){ cerr << “存储分配错 ! \n”; exit(1); } curLen = strlen ( init ); //复制串长度 strcpy ( ch, init ); //复制串值 }

String∷: String({ /构造函数:创建一个空串 ch= new char maxLen+1];/创建串数组 if(ch== NULL)t cer<<“存储分配错!n” exit(1); curlen =u ch|0]=“0
String :: String ( ) { //构造函数:创建一个空串 ch = new char[maxLen+1]; //创建串数组 if ( ch == NULL ) { cerr << “存储分配错!\n”; exit(1); } curLen = 0; ch[0] = ‘\0’; }

提取子串的算法示例 pos=2, len=3 pos=5, len=4 in ty in finity 超出 posten -1 pos+ -1 curLen-l ≥ curlen
提取子串的算法示例 pos+len -1 pos+len -1 curLen-1 curLen i n f i n i t y i n f i n i t y pos = 2, len = 3 pos = 5, len = 4 f i n i t y 超出

String& string operator((int pos, int len)( ∥从串中第pos个位置起连续提取len个字符 /形成子串返回 String* temp= new Strings;动态分配 if(pos≤0‖ posten-1>= maxlen‖lencurLen=0; /返回空串 temp->ch 0=0; eise /提是取子串 if( postlen-1>=curLen) len=curLen- pos;
String& String :: operator ( ) (int pos, int len) { //从串中第 pos 个位置起连续提取 len 个字符 //形成子串返回 String * temp = new String; //动态分配 if (pos= maxLen || lencurLen = 0; //返回空串 temp->ch[0] = '\0'; } else { //提取子串 if ( pos+len -1 >= curLen ) len = curLen - pos;

temp-> curlen=len;/子串长度 for (int i=0,j=pos; ichi=chj];传送串数组 temp-> chen]=“0,;子串结束 return x temp; 例:串st=“ university”,p0s=3,len=4 使用示例 subst=st(3,4) 提取子串 subst=“vers
temp->curLen = len; //子串长度 for ( int i = 0, j = pos; i ch[i] = ch[j]; //传送串数组 temp->ch[len] = ‘\0’; //子串结束 } return * temp; } 例:串 st = “university”, pos = 3, len = 4 使用示例 subSt = st (3, 4) 提取子串 subSt = “vers
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 天津大学:《数据结构 Data Structures》课程教学资源(PPT课件)第十章 内部排序.ppt
- 天津大学:《数据结构 Data Structures》课程教学资源(PPT课件)第六章 树和二叉树.ppt
- 天津大学:《数据结构 Data Structures》课程教学资源(PPT课件)第二章 线性表.ppt
- 天津大学:《数据结构 Data Structures》课程教学资源(PPT课件)第九章 查找.ppt
- 天津大学:《数据结构 Data Structures》课程教学资源(PPT课件)第三章 栈和队列.ppt
- 天津大学:《数据结构 Data Structures》课程教学资源(PPT课件)第一章 绪论(主讲:李晓红).ppt
- 《软件工程 Software Engineering》课程授课教案(PPT课件)第五讲 需求分析建模.ppt
- 《软件工程 Software Engineering》课程授课教案(PPT课件)第四讲 需求分析.ppt
- 《软件工程 Software Engineering》课程授课教案(PPT课件)第二讲 软件过程模型.ppt
- 《软件工程 Software Engineering》课程授课教案(PPT课件)第一讲 软件、软件危机和软件工程.ppt
- 《软件工程 Software Engineering》课程授课教案(PPT课件)第三讲 问题定义和可行性研究.ppt
- 《信息检索》课程教学资源(PPT课件讲稿)第七章 科学研究的信息保障(7-3)学位论文的写作程式.ppt
- 《信息检索》课程教学资源(PPT课件讲稿)第七章 科学研究的信息保障(7-2)学位论文的概念与价值.ppt
- 《信息检索》课程教学资源(PPT课件讲稿)第七章 科学研究的信息保障(7-1)科学研究的概念与方法.ppt
- 《信息检索》课程教学资源(PPT课件讲稿)第六章 信息检索新发展(6-4)网络检索.ppt
- 《信息检索》课程教学资源(PPT课件讲稿)第六章 信息检索新发展(6-3)光盘检索.ppt
- 《信息检索》课程教学资源(PPT课件讲稿)第六章 信息检索新发展(6-2)联机检索.ppt
- 《信息检索》课程教学资源(PPT课件讲稿)第六章 信息检索新发展(6-1)检索数据库.ppt
- 《信息检索》课程教学资源(PPT课件讲稿)第五章 Information Searching Aids(5-5)工具书.ppt
- 《信息检索》课程教学资源(PPT课件讲稿)第五章 Information Searching Aids(5-4)索引.ppt
- 南京大学:《操作系统》课程教学资源(PPT课件)第一章 硬件层.ppt
- 南京大学:《操作系统》课程教学资源(PPT课件)第一章(1.1)操作系统能扩大机器功能.ppt
- 南京大学:《操作系统》课程教学资源(PPT课件)第一章(1.10)处理机管理.ppt
- 南京大学:《操作系统》课程教学资源(PPT课件)第一章(1.11)解决资源冲突的策略和技术.ppt
- 南京大学:《操作系统》课程教学资源(PPT课件)第一章(1.12)微机操作系统的发展.ppt
- 南京大学:《操作系统》课程教学资源(PPT课件)第一章(1.13)分时操作系统特性.ppt
- 南京大学:《操作系统》课程教学资源(PPT课件)第一章(1.2)资源.ppt
- 南京大学:《操作系统》课程教学资源(PPT课件)第一章(1.3)硬件的复杂性.ppt
- 南京大学:《操作系统》课程教学资源(PPT课件)第一章(1.4)算题过程.ppt
- 南京大学:《操作系统》课程教学资源(PPT课件)第一章(1.5)操作录.ppt
- 南京大学:《操作系统》课程教学资源(PPT课件)第一章(1.6)多道程序设计与操作系统的形成.ppt
- 南京大学:《操作系统》课程教学资源(PPT课件)第一章(1.7)While(true).ppt
- 南京大学:《操作系统》课程教学资源(PPT课件)第一章(1.8)Umix的 Shell.ppt
- 南京大学:《操作系统》课程教学资源(PPT课件)第二章(2.1)调试语句.ppt
- 南京大学:《操作系统》课程教学资源(PPT课件)第二章(2.2)非进程内核模型.ppt
- 南京大学:《操作系统》课程教学资源(PPT课件)第二章(2.3)处理器调度.ppt
- 南京大学:《操作系统》课程教学资源(PPT课件)第二章(2.4)负载共享调度算法.ppt
- 南京大学:《操作系统》课程教学资源(PPT课件)OS教学要求.ppt
- 南京大学:《操作系统》课程教学资源(PPT课件)第一章 操作系统概论.ppt
- 南京大学:《操作系统》课程教学资源(PPT课件)第二章 处理器管理.ppt