Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将结构数组的单个成员数组传递给函数以执行线性搜索_C++_Arrays_Struct - Fatal编程技术网

C++ 如何将结构数组的单个成员数组传递给函数以执行线性搜索

C++ 如何将结构数组的单个成员数组传递给函数以执行线性搜索,c++,arrays,struct,C++,Arrays,Struct,我正在尝试对一个包含3个成员的结构数组中的一个成员数组执行线性搜索 我希望它只搜索门牌号,但我搞不懂语法 如果它只是一个数组,我可以这样做,如果它只是一个结构,那么我可以这样做,但因为它是一个结构数组,我很挣扎。你能帮我处理函数头和调用吗 任何帮助都将不胜感激。提前谢谢你 #include <iostream> using namespace std; struct Build { int number; string size; char wall_ty

我正在尝试对一个包含3个成员的结构数组中的一个成员数组执行线性搜索

我希望它只搜索门牌号,但我搞不懂语法

如果它只是一个数组,我可以这样做,如果它只是一个结构,那么我可以这样做,但因为它是一个结构数组,我很挣扎。你能帮我处理函数头和调用吗

任何帮助都将不胜感激。提前谢谢你

#include <iostream>

using namespace std;

struct Build
{
    int number;
    string size;
    char wall_type;
};

int searchArray(const int [], int);


int main ()
{
   int result,                // Index of array that matches search
       value;                // The house number entered by user

   Build house[5] = {
                       {1, "big", 'F'},
                       {3, "small", 'D'},
                       {5, "tiny", 'A'},
                       {7, "huge", 'B'},
                       {9, "medium", 'F'}
                    };

   for (int i = 0; i < 5; i++)              // Just a test to see if the 
   {                                       // array is populated correctly
       cout << house[i].number << "  "
            << house[i].size << "  "
            << house[i].wall_type << endl << endl;
   }

   cout << "Enter the house number:  ";
   cin  >> value;

   result = searchArray(house.number, value);

   if (result == -1)
       cout << "Not found." << endl;
   else
   {
      cout << "Index number:  " << result << endl
           << "House number:  " << house[result].number << endl;
   }

   return 0;
}


/***************************************************************
 *
 *   This function searches an array of integers to find a match
 *   entered by the user.  It works great if you are just dealing
 *   with a single array but I can't figure out the syntax to 
 *   search just one member of a struct in an array of structs.  
 *   Please help!
 *
 */

int searchArray(const int list[], int value)
{
    int index = 0;
    bool found = false;
    int position = -1;

    while (index < 5 && !found)
    {
        if (list[index] == value)
        {
            found = true;
            position = index;
        }
        index++;
    }
    return position;
}
#包括
使用名称空间std;
结构构建
{
整数;
字符串大小;
焦壁型;
};
int搜索数组(常量int[],int);
int main()
{
int result,//与搜索匹配的数组的索引
value;//用户输入的门牌号
建造房屋[5]={
{1,“大”,'F'},
{3,“小”,'D'},
{5,“微小的”,“A'},
{7,“巨大的”,“B'},
{9,“中等”,“F'}
};
for(inti=0;i<5;i++)//只是一个测试,看看
{//数组已正确填充

cout问题在于呼叫。您不能只传递house.number。您可以传递到搜索功能:

  • 整个构建阵列(并根据构建结构适当调整搜索功能)
  • 仅限新整数数组(必须创建一个)
然后你就可以执行搜索了。对我来说,上面的内容甚至不应该编译

您可以尝试:

int intArray[5];

for(int iter=0; iter<5; ++iter){
    intArray[iter] = house[iter].number;
}

int result = searchArray(intArray, value);
int intArray[5];

对于(int iter=0;iterRather)使用
std::vector
std::array
而不是c型数组。您不能只更改int-searchArray(const-int-list[],int-value)int-searchArray(const-Build&list[],it-value)?
list[index].number==value
?当然,首先要使用Marco的更改。另外,当
使用名称空间std;
列表
是标准库中的一个类型时,请注意自己,因此如果它被包含在项目中,它应该与变量名冲突。即使它没有,看起来也会很奇怪。非常感谢@Marco和James的帮助谢谢。这帮了大忙。另外,谢谢你给James关于这个名单的建议。非常感谢。非常有帮助。我会同意你的第一个建议