【问题标题】:Problem with homegrown Concat method国产 Concat 方法的问题
【发布时间】:2011-05-24 19:35:52
【问题描述】:

我已经实现了自己的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();

...它只打印word1 的长度,而不是组合长度。

【问题讨论】:

  • 为了能够帮助您,我们需要确切地知道出了什么问题,只是“不起作用”是一个非常模糊的描述。此外,如果您可以创建一个更短的示例来演示问题(确保它编译并运行),您将获得更多答案。
  • "iv'e 实现我自己的 String 类" - ohgodwhyyyyyy?
  • @Dor S - 在 Compare 成员函数中 - for(; *pSRC==*pSTR &amp;&amp; *pSRC!='\0';pSRC++,pSTR++)。如果 pSRC 指向 abcdpSTR 指向 ab 怎么办?不满足*pSRC != '\0' 的条件,但在调用未定义行为 的第二次迭代后递增pSTR。您总是希望pSRC 大于或等于pSTR 的长度。
  • @jalf :别吓坏了... :-) ...也许是为了学习目的...说真的,谁没有尝试编写 (检查)或字符串对象的个人版本(检查)只是为了学习 C/C++?... :-P ... 现在,如果这是用于生产代码,让我加入“OhRichardDawkinsWhyyyyyyy!!! " (我是无神论者)
  • 您的String 类是否存储自己的长度?您不要在 Concat() 中更改此内容。同样Concat() 采用const char*,而不是另一个String。我希望您将其作为练习,因为在生产代码中没有空间再实现 String

标签: c++ string concat


【解决方案1】:

让我们检查以下代码:

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];
}

查看您的循环条件,然后查看您的 if 条件。显然 else 块永远不会执行,并且你的字符串永远不会被连接。

要解决此问题,应将循环条件替换为 needed_length。然后您必须将string[index] 替换为string[index - original_str_size] 才能在string 中获得正确的索引。

您的代码应如下所示:

int index = 0;
for(; index < needed_length; index++)
{
    if(index < original_str_size)
        m_str[index] = str_copy[index];
    else
        m_str[index] = string[index - original_str_size];
}

顺便说一句,str_copy 指向什么?它是有效的记忆吗? del() 释放内存了吗?可能想检查一下。

【讨论】:

  • 我已将代码更改为您的答案代码。还是行不通。我的 del() 代码是这样的: void String::del() { if(m_str != 0) { delete []m_str; m_str = 0; m_size = 0; } }
  • 好吧,那么你也删除了 str_copy 指向的字符串。完成后不要调用 del() 再调用delete[] str_copy
【解决方案2】:

在您的 Concat 函数中,您似乎正在删除包含原始字符串的内存,然后将字符串从该内存复制到新分配的内存中。

【讨论】:

  • 那么我怎样才能将 sring 复制到分配的东西上以便它可以工作?
【解决方案3】:

在比较中,您有一个 ;在 for 循环之后,这意味着循环什么也不做。当第一个字符匹配时,您也会返回 0。

在 Concat 中,您正在创建 str_copy = m_str,然后可能会删除 m_str 并创建一个新的 m_str。然后你从删除的 m_str 复制到新的 m_str,你可能会很幸运,但我不会依赖这个。

【讨论】:

  • 比较没问题。关于联系人 - 我应该怎么做才能解决这个“不是 4 肯定”的事情?
  • 您需要先获取原始 m_str 的副本。或者换句话说,创建一个临时的 char * 并添加正确的数量。完成所有的复制和附加(同时注意@Marlon),然后删除 m_str,然后使 m_str = 临时。
  • 另外,比较也不好。
  • 现在您已经编辑了标题错误的问题。而且我的答案的很多上下文都不存在。
  • iv'e 更改代码,现在我用原始字符串初始化“副本”.. 像这样: String str_copy(m_str); ……这样写对吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-27
  • 2012-05-22
  • 2023-03-14
  • 2021-06-12
  • 1970-01-01
  • 1970-01-01
  • 2020-08-09
相关资源
最近更新 更多