试图从数组复制复制时的分割故障(核心倾倒)错误

发布于 2025-02-04 06:07:42 字数 519 浏览 3 评论 0原文

试图将内容从B复制到A中,但我得到了这个错误 有人告诉我,这意味着我正在尝试访问我不允许的记忆,但是我不知道该怎么做才能使其编译。

替换(txt,code);

string replace(string a , string b)
{
    string alpha[26] = {"abcdefghijklmnopqurstuvwxyz"};

    for (int i = 0; i < strlen(a); i++)
    {
        for(int n = 0; n < 26; n++)
        {
            if(a[i] == alpha[n])
            {
                a[i] = b[n];
                i++;
            }
        }
    }
    return word;
}

我是初学者,所以对干净的编码或句法糖或类似的东西没有评论,请帮助我解决这个问题

trying to copy stuff from b into a but i get that error
someone told me it means i'm trying to access memory that i'm not allowed to, but i don't know what should i do to make it compile.

replace(txt , code);

string replace(string a , string b)
{
    string alpha[26] = {"abcdefghijklmnopqurstuvwxyz"};

    for (int i = 0; i < strlen(a); i++)
    {
        for(int n = 0; n < 26; n++)
        {
            if(a[i] == alpha[n])
            {
                a[i] = b[n];
                i++;
            }
        }
    }
    return word;
}

i'm a beginner so no comments about clean coding or syntactic sugar or stuff like that just help me resolve this please

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆流 2025-02-11 06:07:42

看来您在 pointers 上,我建议您建议您来了解它们。还考虑阅读有关 datatypes sTL的类型您正在使用。 (原因std :: string已经是一个值,因此,当您创建std :: string [26]时,您实际上是在创建指针指向

std::string replace(string a , string b)
{
    std::string alpha = {"abcdefghijklmnopqurstuvwxyz"};

    for (size_t i = 0; i < a.size(); ++i)
    {
        for(size_t n = 0; n < alpha.size(); ++n)
        {
            if(a[i] == alpha[n])
            {
                a[i] = b[n];
                i++; // Also, I think you doesnt need this line, cause you already incrementing i in for loop
            }
        }
    }
    return a;
}

指针 字符串上使用的strlen(),这也不是真正正确的,因为它用于char值。如果您要获得字符串的长度,则最好使用string.lenght()

,也最好使用size_tnofeged> size>无签名的int int在这种情况下,而不是int,因为您不需要负数即可parce这些字符串。 ()

It looks like you have some problems with understending pointers, so I recommend you to read about them. Also consider reading about datatypes and types from STL you are using. (cause std::string is already an array of values so, when you are creating std::string[26], you actually are creating pointer to a pointer)

I guess you have are trying to do something like that:

std::string replace(string a , string b)
{
    std::string alpha = {"abcdefghijklmnopqurstuvwxyz"};

    for (size_t i = 0; i < a.size(); ++i)
    {
        for(size_t n = 0; n < alpha.size(); ++n)
        {
            if(a[i] == alpha[n])
            {
                a[i] = b[n];
                i++; // Also, I think you doesnt need this line, cause you already incrementing i in for loop
            }
        }
    }
    return a;
}

Also you have used strlen() on your string, that also is not really correct, cause it is used on char values. If you whant to get length of a string it is better to use string.lenght()

Also, It is better to use size_t or unsigned int instead of int in this case, cause you don't need negative numbers in order to parce these strings. ()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文