作为抽象数据类型的数组顺序表稀疏矩阵字符串.pptVIP

  • 0
  • 0
  • 约1.53万字
  • 约 67页
  • 2020-11-07 发布于广东
  • 举报

作为抽象数据类型的数组顺序表稀疏矩阵字符串.ppt

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 ) { //复制构造函数:从已有串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 ) { //复制构造函数: 从已有字符数组*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 ) { cerr “存储分配错!\n”; exit(1); } curLen = 0; ch[0] = ‘\0’; } 提取子串的算法示例 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 String; //动态分配 if (pos0 || pos+len-1 = maxLen || len0) { temp-curLen = 0; //返回空串 temp-ch[0] = \0; } else { //提取子串 if ( pos+len -1 = curLen ) len = curLen - pos; temp-curLen = len; //子串长度 for ( int i = 0, j = pos; i len; i++, j++ ) temp-ch[i] = ch[j]; //传送串数组 temp-ch[len] = ‘\0’; //子串结束 } return * temp; } 例:串 st = “university”, pos = 3, len = 4 使用示例 subSt = st (3, 4) 提取子串 subSt = “vers” String String :: operator = ( String ob ) { //串赋值:从已有串ob复制 if ( ob != this ) {

文档评论(0)

1亿VIP精品文档

相关文档