gpt4 book ai didi

c++ - 信件取出

转载 作者:行者123 更新时间:2023-11-30 05:04:38 25 4
gpt4 key购买 nike

我想创建一个函数,将字符串数组中的某些字母替换为我选择的字母,我应该在不使用 c++ 库中的任何函数模板的情况下执行此操作。到目前为止,我有这个,但是当我打印输出时,它似乎并没有改变数组元素。有人可以帮助我吗?
我试过的示例数组是:

string replace[7] = { "gavel" , "apple" , "bear" , "flask" , "festival" , "sacrifice" , "lava" };

然后我尝试将数组中的 (a) 更改为 (z)。

int remove(string array1[], int n, char a, char c)
{
int q = 0;
int index = 0;
string s;
char c = s[index];

if (n > 1)
{
for (q = 0; q < n; q++)
{
s = array1[q];
size_t len = s.size();

for (index = 0; index < len; index++)
{
if (s[index] == a)
{
s[index] == c;
}
}
cout << array1[q] << endl;
}
}

else
{
return (-1);
}
}

最佳答案

您的代码中有 2 个问题:

  1. s = array1[i]; 创建字符串的拷贝。然后您继续更改拷贝,而不是数组中的原始字符串。
  2. s[index] == letterToFill; 你错误地使用了逻辑比较,而不是赋值

尝试将 for 循环内的 s 重新定义为字符串引用:

...
for (i = 0; i < n; i++)
{
string& s = array1[i];
...

关于c++ - 信件取出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48779984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com