【问题标题】:Linear Search returning array with indices value is found at具有索引值的线性搜索返回数组位于
【发布时间】:2013-02-27 23:54:17
【问题描述】:

我尝试了一个程序来返回一个数组,其中包含找到特定输入值的数组索引,但每次运行都会导致错误,这似乎是无限的运行时间。该错误似乎是在打印出找到的最后一个索引后立即发生的。

有人可以帮忙吗? (旁注:我已经看过多页关于删除指针的内容;我应该在这里这样做吗?)

忘了提 - 我希望返回数组的第一个槽保存数组的大小,以便稍后在程序中轻松访问它

#include <iostream>
#include <vector>
using namespace std;

int* linearSearch(int* n, int k, int f) {
    // Input: Index 0 Address ; Size of Array; Element to Search
    // Output: Array of Found Indicies
    vector <int> a;
    int* b;
    for(int i = 0; i < k; i++)
        if(n[i] == f)
            a.push_back(i);
    *b = a.size();
    for(int i = 0; i < a.size(); i++)
        b[i + 1] = a[i];
    return b;
}

int main() {
    int c[10] = {4, 4, 6, 3, 7, 7, 3, 6, 2, 0};
    int* k = linearSearch(&c[0], sizeof(c)/sizeof(int), 4);
    for(int i = 0; i < k[0]; i++) {
        cout << "Found at index: " << k[i + 1] << endl;
    }
    return 0;
}

【问题讨论】:

  • 你认为*b = a.size(); 行是什么?
  • 忘了提——我希望返回数组的第一个槽保存数组的大小,以便稍后在程序中轻松访问。
  • 那么你在哪里分配空间让 b 指向呢?

标签: c++ algorithm function search pointers


【解决方案1】:
int* b;
....
*b = a.size();

b 必须被分配。请尝试以下操作:

int* b = new int[a.size() + 1];
b[0] = a.size();

我明白你的意思。 b 在第一个元素中会有神奇的长度。这在 Pascal/Delphi 中是这样,但在 C/C++ 中不是这样。

【讨论】:

  • “必须分配 b”是什么意思?我会尝试一下,看看它是否有效,但你能解释一下为什么它有效而我的无效吗?
  • @JohnJazzer 没有冒犯,但分配(更一般的内存管理)是 C/C++ 中的核心概念之一。在开始编码之前阅读一些书籍或至少是教程会更好吗?更具体地说,int* b; 的意思是“创建一个能够用整数指向内存位置的变量”。美好的!但是没有为数组分配内存空间,你必须这样做,因此分配。
  • 啊,很漂亮——它奏效了。现在弄清楚第一行是什么意思
  • @John:这是因为b 一开始是一个全新的指针,指向绝对无处(或任何地方)。这样的野指针对任何事情都没有用。
  • 啊这确实有道理 - 我是 C++ 新手,所以我会尝试阅读它。谢谢!
【解决方案2】:

您正在写入您从未声明过的堆内存。

int* b;

这个指针从未被初始化,指向一个未定义的内存地址。然后,当您使用索引运算符分配匹配项时,您将写入未定义内存地址之后的后续字节。

您需要使用“new[]”运算符分配空间来存储结果。此外,如果您正确地声明了内存,您会将匹配结果的数量分配给结果数组中的第一个元素 - 这似乎不是您的意图。

看看 C++ 中使用 new [] 运算符的动态内存分配。

【讨论】:

    【解决方案3】:

    如果你仍然使用 std::vector,为什么不在最需要的地方使用它呢?另外,如果您不打算通过该指针修改数组,请通过 const 指针表示:

    std::vector<int> linearSearch(const int* n, int k, int f)
    {
       std::vector<int> res;
       for(int i = 0; i < k; i++)
            if(n[i] == f) res.push_back(i);
       return res;
    }
    
    int main() {
        int c[10] = {4, 4, 6, 3, 7, 7, 3, 6, 2, 0};
        std::vector<int> k = linearSearch(&c[0], sizeof(c)/sizeof(int), 4);
        for(int i = 0; i < k.size(); i++) {
            cout << "Found at index: " << k[i] << endl;
        }
        return 0;
    }
    

    【讨论】:

      【解决方案4】:

      这并不完美,但它更接近于正确的实现,您应该能够通过一些工作进一步改进:

      #include <iostream>
      #include <vector>
      using namespace std;
      
      std::vector<int> linearSearch(int* n, int k, int f)
      {
        vector <int> a;
      
        for(int i = 0; i < k; i++)
        {
            if(n[i] == f)
            {
                a.push_back(i);
            }
        }
      
        return a ;
      }
      
      int main() {
        int c[10] = {4, 4, 6, 3, 7, 7, 3, 6, 2, 0};
        std::vector<int> result = linearSearch(&c[0], sizeof(c)/sizeof(int), 4);
      
        for(unsigned int i = 0; i < result.size(); i++)
        {
            cout << "Found at index: " << result[i + 1] << endl;
        }
        return 0;
      }
      

      【讨论】:

      • 为什么不直接返回 std::vector 而不是通过引用传递它?
      • 好点,我想我纯 C 编程时代的旧习惯,让我调整一下。
      猜你喜欢
      • 2019-03-02
      • 1970-01-01
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      • 2019-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多