【问题标题】:How do I linear search two arrays in c++?如何在 C++ 中线性搜索两个数组?
【发布时间】:2020-05-31 11:09:41
【问题描述】:

我有两个数据类型为 double 的数组 - 称为 array1[10] 和 array2[8]。我需要使用线性搜索函数在每个元素的 array1 中搜索 array2。函数声明是

string linSearch (double array1[10], double array2[8]);

如果在array1 中找到了array2,那么我需要打印出它在array1 中的位置的索引。如果找不到,我需要输出为“NA”。此输出必须是分隔的逗号字符串。 例如。

//given the two arrays:
array1={1.1,1.2,6,7,3.5,2,7,8.8,9,23.4}
array2={6,45,2,7,1.1,5,4,8.8}

//after the linear search completes, the output must be the index in which //array2 is found in array1. if its not found, then it must be NA:

2,NA,5,6,0,NA,NA,7

到目前为止,我有以下代码。这是我第一次使用数组,但我仍然难以理解这个概念——比如一旦我定义了函数,我什至如何在主程序中调用它?!无论如何..我的函数定义(不包括主程序)是:

string linSearch (double array1[10], double array2[8])
{
    int index1 = 0;
    int index2 =0;
    int position =-1;
    bool found = false;
    while (index1<10 && !found && index2<8)
    {
        if array1[index1] == array2[index2])
        {
            found = true;
            position = index1;
        }
        index1++;
        index2++;
    }

    return position;

}

对于在另一个数组中搜索一个数组以及如何输出分隔列表以及如何将其连接到我的主程序,我感到非常困惑。任何帮助将不胜感激。谢谢!

【问题讨论】:

  • 第二个数组的起始位置是什么?
  • 同样在您当前的代码中,即使 array2 中只有一个数字在数组 1 中,您也会返回
  • 不,位置是记录搜索值的位置,变量index2是第二个数组的起始位置。
  • @N7c 在第二个中找到一个时显示数组示例。
  • 请添加示例输入和示例输出,目前尚不清楚如何从大小为 10 和 8 的数组中获得 4 个条目 (2,NA,6,7) 的输出

标签: c++ arrays function cout linear-search


【解决方案1】:
#include <iostream>
using namespace std;

string linSearch(double array1[10], double array2[8])
{

    string result = "";
    bool found;
    for (int j = 0; j < 8; j++)
    {
        if(j > 0)
            result.append(", ");

        found = false;
        for (int i = 0; i < 10; i++){
            if (array1[i] == array2[j]) {
                result.append(to_string(i));
                found = true;
            }
        }
        if(!found)
            result.append("NA");
    }
    return result;
}

int main(){

    double a1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    double a2[8] = {11, 25, 3, 41, 5, 6, 7, 8};
    cout << linSearch(a1, a2) << endl;
    return 0;
}

【讨论】:

    【解决方案2】:

    您不是在另一个数组中搜索一个数组。您正在从第二个数组中的一个数组中搜索元素。如果您使用线性搜索并且不想对数组进行排序,则需要 2 个嵌套循环来执行此操作。一个用于第二个数组中的每个元素,一个用于在第一个数组中查找该元素。

    保持简单,从查找单个元素在一个数组中的位置开始。因为您正在比较doubles,所以您不应该将它们与== 进行比较。接下来,您只需要一个为第二个数组中的每个元素调用第一个的函数:

    #include <string>
    #include <vector>
    #include <algorithm>    
    #include <iostream>
    
    
    void index_to_string(const std::vector<double>& v,double e,std::ostream& out){
        auto it = std::find_if(v.begin(),
                               v.end(),
                               [e](const double& x) {
                                   return std::abs(e-x) < 1e-8;
                               }
                            );
        if (it == v.end()) {
            out << "NA";
        } else {
            out << (it - v.begin());
        }
    }
    
    void all_indices_to_string(const std::vector<double>& v1,const std::vector<double>& v2,std::ostream& out){
        if (v1.size() == 0 || v2.size()==0) return;
        index_to_string(v1,v2[0],out);
        for (size_t i=1;i<v2.size();++i){
            out << ",";
            index_to_string(v1,v2[i],out);
        }
    }
    
    int main() {
        double array1[] ={1.1,1.2,6,7,3.5,2,7,8.8,9,23.4};    
        double array2[] ={6,45,2,7,1.1,5,4,8.8};
        all_indices_to_string(
            std::vector<double>(std::begin(array1),std::end(array1)),
            std::vector<double>(std::begin(array2),std::end(array2)),
            std::cout
        );
    }
    

    输出:

    2,NA,5,3,0,NA,NA,7
    

    【讨论】:

    • 非常感谢您。不幸的是,作为分配要求,我们不允许使用向量
    • @N7c 真可惜。您可以将其作为练习来重构代码以首先用开​​始和结束迭代器替换向量参数,然后将指针传递给数组中的第一个/最后一个元素只是一个很小的步骤。顺便说一句,如果有这样的要求,你应该在问题中提到它,因为不使用向量基本上是不使用 c++
    【解决方案3】:

    在您的数组示例和预期输出中

    //given the two arrays:
    array1={1.1,1.2,6,7,3.5,2,7,8.8,9,23.4}
    array2={6,45,2,7,1.1,5,4,8.8}
    

    2,NA,5,6,0,NA,NA,7
           ^
    

    有一个错字。输出应该是

    2,NA,5,3,0,NA,NA,7
           ^
    

    因为数字 7 位于数组 array1 的第三个位置。

    你来了。

    #include <iostream>
    #include <string>
    #include <sstream>
    
    std::string linearSearch( const double a1[], size_t n1, const double a2[], size_t n2 )
    {
        std::ostringstream os;
    
        for ( size_t i = 0; i < n2; i++ )
        {
            if ( i != 0  ) os << ',';
    
            size_t j = 0;
    
            while ( j < n1 && a2[i] != a1[j] ) ++j;
    
            if ( j == n1 ) os << "NA";
            else os << j;
        }
    
        return os.str();
    }
    
    
    int main() 
    {
        double a1[] = { 1.1, 1.2, 6, 7, 3.5, 2, 7, 8.8, 9, 23.4 };
        const size_t N1 = sizeof( a1 ) / sizeof( *a1 );
        double a2[] = { 6, 45, 2, 7, 1.1, 5, 4, 8.8 };
        const size_t N2 = sizeof( a2 ) / sizeof( *a2 );
    
        std::cout << linearSearch( a1, N1, a2, N2 ) << '\n';
    
        return 0;
    }
    

    程序输出是

    2,NA,5,3,0,NA,NA,7
    

    【讨论】:

      猜你喜欢
      • 2018-05-23
      • 1970-01-01
      • 2020-09-26
      • 1970-01-01
      • 1970-01-01
      • 2016-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多