一个c++类的问题

Buchanan 2008-02-29 04:56:11
这是面试题让我写的类,hr说“GetLength你是数string的长度,但应该直接返回sizeStr,不仅速度会快很多,这也是用Class 来实现的意义之一。”
这话是什么意思啊?不数string的长度能直接返回sizeStr么?

#include <stdio.h>

class MyString{
public:
MyString();
MyString(char *str) //consturct the string
{
pStr=str;
}
~MyString(){};

int GetSizeStr();
void SetLength(int len);
int GetLength();
void Output(char *deal);
void Add(char add);
void Append(char *append);
int Find(char find); //find a char
int Find(char *strFind); //find a string

private:
char *pStr;
int sizeStr;
};

int MyString::GetLength() //get the length of string
{
sizeStr=0;
char *p=pStr;
while (*p!='\0')
{
sizeStr++;
p++;
}
return sizeStr;
}

void MyString::SetLength(int len) //set the length
{
sizeStr=len;
char *p=pStr;
while (len>0) //keep the string[0~sizeStr-1]
{
p++;
len--;
}
*p='\0';
p++;
while (*p!='\0') //set the char after the new length as '\0'
{
*p='\0';
p++;
}
printf("The string after setting length is: %s\n",pStr);
}

int MyString::GetSizeStr() //get sizeStr
{
return sizeStr;
}

void MyString::Output(char *deal) //output the string
{
printf("The string after %s is: %s\n",deal,pStr);
}

void MyString::Add(char add) //append a char to the end of string
{
char *p=pStr;
while (*p!='\0') p++;
*p=add;
}

void MyString::Append(char *append) //append a char to the end of string
{
char *p=pStr;
char *pApp=append;
while (*p!='\0') p++;
while (*pApp!='\0')
{
*p=*pApp; //append the string char by char
pApp++;
p++;
}
}

int MyString::Find(char find) //find char
{
int pos=0;
char *p=pStr;
while (*p!=find&&*p!='\0') //find through the pStr
{
p++;
pos++;
}
if (*p==find) return pos; //found the char
else return -1;
}

int MyString::Find(char *strFind) //find string
{
int pos=0;
char *p=pStr;
char *pTextFind=strFind;
while (*p!='\0')
{
if (*strFind==*p) //found the first char is matched
{
char *pSearch=p;
pTextFind=strFind; //reset the pointer point to string wanna find
while (*pSearch==*pTextFind&&*pSearch!='\0') //matching
{
pSearch++;
pTextFind++;
}
if (*pTextFind=='\0') return pos; //match all of strFind, found the string
}
pos++;
p++;
}
return -1;
}


main()
{
char strInput[999]=""; //the string input
char append[999]=""; //the string want to append to the end
char strFind[999]=""; //a string want to find in the string
char add;
char find;
int len=0;

printf("Input a String for initilizing: ");
scanf("%s",strInput);
MyString myString(strInput); //initilize the string

printf("The length of the string is %d",myString.GetLength()); //test of GetLength

printf("\n\nPlease input the length you want to set:"); //test of SetLength
scanf("%d",&len);
myString.SetLength(len);
printf("The length has been set to: %d\n",myString.GetSizeStr());

printf("\nInput the char you want to append to the end of string:"); //test of Add
scanf("%s",&add);
myString.Add(add);
myString.Output("ADDED is: ");

printf("\nInput the string you want to append to the end of string:"); //test of Append
scanf("%s",append);
myString.Append(append);
myString.Output("APPEND is: ");

printf("\nInput the char you want to find:"); //test of find(char f)
scanf("%s",&find);
printf("The position of the CHAR you want to find is %d\n",myString.Find(find));

printf("\nInput the string you want to find:"); //test of find(char *str)
scanf("%s",strFind);
printf("The position of the STRING you want to find is %d\n",myString.Find(strFind));
return 0;
}
...全文
67 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
iambic 2008-02-29
  • 打赏
  • 举报
回复
记下来,就用不着每次都手动数了。
Buchanan 2008-02-29
  • 打赏
  • 举报
回复
还是不明白。
hxxwcc 2008-02-29
  • 打赏
  • 举报
回复
you can modify the length of a string(stored in an int number) each time you modified the length of string to avoid computing the length each time.
Buchanan 2008-02-29
  • 打赏
  • 举报
回复
sizeStr直接是string的长度?不用计算?
p0303230 2008-02-29
  • 打赏
  • 举报
回复
不是有sizestr
还要用函数干嘛啊
独孤过儿 2008-02-29
  • 打赏
  • 举报
回复
可以在类里面加个计数器...

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧