如何在 c++ 中线性搜索两个数组?

How do I linear search two arrays in c++?

本文关键字:两个 数组 c++ 线性搜索      更新时间:2023-10-16

我有两个数据类型为双精度的数组 - 称为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;
}

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

#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 个嵌套循环来执行此操作。一个用于第二个数组中的每个元素,一个用于在第一个数组中查找该元素。

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

#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

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

//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