【问题标题】:find and replace a specific element of an array查找和替换数组的特定元素
【发布时间】:2017-10-01 15:51:18
【问题描述】:

我正在尝试在数组 Bob 中找到“空”,然后在找到索引中的空位置后,我想用“LEEROY JENKINS”替换单词“空”请原谅我缺乏知识,我仍然很对此不熟悉,并尽我所能学习。

问题:

  1. int spindex() 没有找到正确的索引
  2. empty 没有被替换

#include <string>
#include <iostream>

using namespace std;

int spindex(string x[], int n)
{
    int i = 0;
    int tindex;

    while (x[i] != "empty" && i < n)
    {
        tindex = i;
        i++;
    }
    return tindex;
}

int main()
{
    string Bob[] = { "shaun", "empty", "tom", "empty", "chris", "sharon", "empty"};
    int pim = 7;
    int Q = spindex(Bob, pim);
    Bob[Q] = "LEEROY JENKINS";
    cout << Q << endl;
    cout << Bob[Q] << endl;
    for (int i = 0; i < 7; i++) {
        cout << Bob[i] << endl;
    }
}

【问题讨论】:

  • 您的pim 值错误。为什么?
  • 让它更短忘记改变它,但不会改变结果只是一个小的错过类型
  • 如果您的输入中没有“空”,它会极大地改变结果。

标签: c++ arrays string while-loop


【解决方案1】:

问题在于您构建循环的方式:

while (x[i] != "empty" && i < n)
{
    tindex = i;
    i++;
}

首先,i 为 0。tindex 设置为 0,i 设置为 1。

然后循环重复,检查条件。哎呀! x[1] 是“空的”!循环结束,tindex 仍为 0。

这就是 Q 为 0 以及替换错误元素的原因。

此外,您在尝试使用x[i] 之后检查i &lt; n (哎呀)。

你也可以直接返回i,因为这已经是你需要的值了:

int spindex(string x[], int n)
{
    int i = 0;

    while (i < n && x[i] != "empty") {
        i++;
    }

    return i;
}

(live demo)


在我看来,整个问题最好这样构造:

#include <string>
#include <algorithm>
#include <iostream>

int main()
{
    std::string Bob[] = { "shaun", "empty", "tom", "empty", "chris", "sharon", "empty"};

    auto it = std::find(std::begin(Bob), std::end(Bob), "empty");
    if (it == std::end(Bob)) {
        // No "empty" to replace!
        return EXIT_FAILURE;
    }

    // Replace first "empty" with "BOB"
    *it = "BOB";

    // Here is our array now
    std::cout << '[';
    for (const auto el : Bob)
        std::cout << el << ',';
    std::cout << ']' << '\n';
}

// Output: [shaun,BOB,tom,empty,chris,sharon,empty,]

(live demo)

【讨论】:

  • 你能建议我如何解决这个问题吗?
  • @ChronicArrow:我已经做到了。我用文字说出来,并提供了一个现场演示的链接。作为奖励,我现在还提供了一个全新的解决方案。
  • 非常感谢我的垃圾互联网没有让我及时了解你所说的!我的道歉
  • @ChronicArrow:不客气。不要忘记点赞和接受! :)
【解决方案2】:

这个函数

int spindex(string x[], int n)

{
    int i = 0;
    int tindex;

    while (x[i] != "empty" && i < n)
    {
        tindex = i;
        i++;
    }
    return tindex;
}

错了。除非数组的第一个元素的值为 "empty",否则该函数会返回包含该字符串的元素的索引之前的索引。

另外while循环的条件中子条件的顺序也是错误的。

这样写

size_t spindex( const std::string a[], size_t n )
{
    size_t i = 0;

    while ( i < n  && a[i] != "empty" ) i++;

    return i;
}

然后这样称呼它

size_t Q = spindex( Bob, sizeof( Bob ) / sizeof( *Bob ) );

更通用的函数编写方法如下所示

size_t spindex( const std::string a[], size_t n, const std::string &s )
{
    size_t i = 0;

    while ( i < n  && a[i] != s ) i++;

    return i;
}

而且可以这样称呼

size_t Q = spindex( Bob, sizeof( Bob ) / sizeof( *Bob ), "empty" );

作为替代方案,您可以使用标头 &lt;algorithm&gt; 中声明的标准算法 std::find

【讨论】:

    猜你喜欢
    • 2022-06-21
    • 2015-07-26
    • 1970-01-01
    • 2017-09-12
    • 2019-06-23
    • 1970-01-01
    • 2015-10-11
    • 2012-10-15
    • 2011-07-25
    相关资源
    最近更新 更多