国产Concat方法的问题
我已经实现了自己的 String
类,并且需要编写 Concat
方法。
我无法让它发挥作用。
我的代码是:
//the m_str is private member which is initialize in the c-tor
//this function is get a string and concat it with the original string
String &String::Concat(const char *string)
{
int original_str_size = length(m_str);
int other_str_size = length(string);
int needed_length = original_str_size + other_str_size + 1;
char *str_copy = m_str;
del();
m_str = new char[needed_length];
m_size = needed_length;
int index = 0;
for(; index < original_str_size; index++)
{
if(index < original_str_size)
m_str[index] = str_copy[index];
else
m_str[index] = string[index];
}
m_str[index] = 0;
return *this;
}
Concat
方法中的问题是我写了类似的内容:
String word3 = word1.Contact(word2);
它应该使 word3
类似于 word1+word2
但是当我运行该程序时失败了。
当我写道:
cout << word1.Contact(word2).Length();
...它只打印单词
1的长度,而不是组合长度。
I've implemented my own String
class, and need to write Concat
method.
I can't get it work.
My code is:
//the m_str is private member which is initialize in the c-tor
//this function is get a string and concat it with the original string
String &String::Concat(const char *string)
{
int original_str_size = length(m_str);
int other_str_size = length(string);
int needed_length = original_str_size + other_str_size + 1;
char *str_copy = m_str;
del();
m_str = new char[needed_length];
m_size = needed_length;
int index = 0;
for(; index < original_str_size; index++)
{
if(index < original_str_size)
m_str[index] = str_copy[index];
else
m_str[index] = string[index];
}
m_str[index] = 0;
return *this;
}
The problem in the Concat
method is that I wrote something like:
String word3 = word1.Contact(word2);
It is supposed to make word3
to be like word1+word2
but the program failed when I ran it.
When I wrote:
cout << word1.Contact(word2).Length();
...it printed only the word
1's length, instead of the combined length.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让我们检查以下代码:
查看循环条件,然后查看 if 条件。显然,else 块永远不会执行,并且您的字符串永远不会连接。
要解决此问题,您的循环条件应替换为
needed_length
。然后,您必须将string[index]
替换为string[index - original_str_size]
才能在string
中获取正确的索引。您的代码应如下所示:
顺便说一句,
str_copy
指向什么?是有效记忆吗?del()
是否释放了内存?可能想检查一下。Let's examine the following code:
Look at your loop condition, then look at your if condition. Clearly the else block will never execute, and your string is never concatenated.
To fix this problem, your loop condition should be replaced with
needed_length
. Then you would have to replacestring[index]
withstring[index - original_str_size]
to get the correct index instring
.Your code should look like this:
On a side note, what does
str_copy
point to? Is it valid memory? Diddel()
free the memory? Might want to check that.在您的 Concat 函数中,看起来您正在删除包含原始字符串的内存,然后将字符串从该内存复制到新分配的内存中。
In the your Concat function, it looks like you are deleting the memory which contains your original string before copying the string out of that memory into the newly allocated memory.
在比较中,您有一个 ;在 for 循环之后,这意味着循环不执行任何操作。当第一个字符匹配时,您还将返回 0。
在 Concat 中,您将创建 str_copy = m_str,然后可能删除 m_str 并创建一个新的 m_str。然后你从删除的 m_str 复制到新的 m_str,你可能会很幸运,但我不会依赖于此。
In Compare, you have a ; after the for loop, this means the loop does nothing. Also you are returning 0 when the first character matches.
In Concat, you are making str_copy = m_str, then presumably deleting m_str and creating a new m_str. then you copy from the deleted m_str to the new m_str, you may get lucky, but I would not rely on this.