试图从数组复制复制时的分割故障(核心倾倒)错误
试图将内容从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您在 pointers 上,我建议您建议您来了解它们。还考虑阅读有关 datatypes 和 sTL的类型您正在使用。 (原因std :: string已经是一个值,因此,当您创建std :: string [26]时,您实际上是在创建指针指向
)
指针 字符串上使用的
strlen()
,这也不是真正正确的,因为它用于char值。如果您要获得字符串的长度,则最好使用string.lenght()
,也最好使用
size_t
或nofeged> 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:
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 usestring.lenght()
Also, It is better to use
size_t
orunsigned int
instead ofint
in this case, cause you don't need negative numbers in order to parce these strings. ()