作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想创建一个函数,将字符串数组中的某些字母替换为我选择的字母,我应该在不使用 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 个问题:
s = array1[i];
创建字符串的拷贝。然后您继续更改拷贝,而不是数组中的原始字符串。s[index] == letterToFill;
你错误地使用了逻辑比较,而不是赋值尝试将 for 循环内的 s
重新定义为字符串引用:
...
for (i = 0; i < n; i++)
{
string& s = array1[i];
...
关于c++ - 信件取出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48779984/
是How to Design Programs一书中的一段数据定义(注释),讲授Scheme-like语言和DrRacket IDE。 [Maybe #, Letter] 中的#, 是什么意思? ;
我是一名优秀的程序员,十分优秀!