简体   繁体   English

如何在 c++ 中的 function 中将数据结构数据数组作为参数传递

[英]How to pass data structure data array as a parameter in a function in c++

So I declared this structure data:所以我声明了这个结构数据:

struct Auxiliary
        {
          char come_greet[100], leave_greet[100], aux_words[100];  
        };
        Auxiliary j[1000];

And now I need to use this data in a function but I can t pass the structure as a parameter:现在我需要在 function 中使用这些数据,但我不能将结构作为参数传递:

void search_for_val_name(struct StructName & y[] ,char name1[],char answer[], int mm)
    {
        for(int i=0;i<mm;i++)
            {
                if(strchr(answer,(*y).name1))
                    return (*y).name1;
            }
    }

How can I make this function work properly?我怎样才能使这个 function 正常工作?

First of all, this is C++, so all those fixed-size arrays are just a buffer overflow in waiting - with structures so large, there's no performance benefit from doing it that way.首先,这是 C++,所以所有那些固定大小的 arrays 只是等待中的缓冲区溢出 - 结构如此之大,这样做没有性能优势。

Thus, I'd start as follows.因此,我将按如下方式开始。 Whenever an unknown number of characters is desired, std::string is used.每当需要未知数量的字符时,就使用std::string Whenever an unknown number of elements of some other type is desired, std::vector is used.每当需要某种其他类型的未知数量的元素时,就使用std::vector

#include <string>
#include <vector>

struct Auxiliary {
    std::string come_greet, leave_greet;
    std::vector<std::string> aux_words; // if this is just one word, don't call it words!

    bool operator==(const Auxiliary &o) const {
        return &o == this ||
            o.come_greet == come_greet &&
            o.leave_greet == leave_greet &&
            o.aux_words == aux_words;
    }
};

struct Outer {
    static Auxiliary not_found;

    std::vector<Auxiliary> j; // j is a terrible name - choose something with meaning!

    Auxiliary &find_answer(std::string Auxiliary::*field, const std::string &answer);
};

Then, the search is a method: it doesn't need to have the mm argument since it knows the size of the j vector, it doesn't need to be fed j since it has direct access to it, and we can actually write C++, not C.然后,搜索是一种方法:它不需要mm参数,因为它知道j向量的大小,不需要输入j ,因为它可以直接访问它,我们实际上可以写C++,而不是 C。 The field argument specifies which member of the Auxiliary structure is meant to be searched, eg &Auxiliary::come_greet . field参数指定要搜索Auxiliary结构的哪个成员,例如&Auxiliary::come_greet Also note the use of std::string::find instead of strstr .另请注意使用std::string::find而不是strstr find_answer returns a reference to an Auxiliary , since returning references is cheap. find_answer返回对Auxiliary引用,因为返回引用很便宜。 It could also return a value (ie Auxiliary , not Auxiliary& ), but that would copy the value, most likely unnecessarily.它也可以返回一个(即Auxiliary ,而不是Auxiliary& ),但这很可能会不必要地复制该值。

Auxiliary Outer::not_found;

Auxiliary& Outer::find_answer(std::string Auxiliary::*field, const std::string &answer) {
  for (auto &aux : j)
    if ((aux.*field).find(answer) != std::string::npos)
      return aux;
  return not_found;
}

If you wouldn't need to modify Auxiliary via the returned reference, the returned type should be const Auxiliary& .如果您不需要通过返回的引用修改Auxiliary ,则返回的类型应该是const Auxiliary&

Finally, a little test that demonstrates the behavior:最后,一个演示该行为的小测试:

#include <cassert>

int main() {
    Outer outer;
    outer.j = {
                {"come0", "leave0", {"auxa_0", "auxb_0"}},
                {"come1", "leave1", {"auxa_1"}}
            };
    assert(outer.find_answer(&Auxiliary::come_greet, "foo") == Outer::not_found);
    assert(outer.find_answer(&Auxiliary::come_greet, "come0") == outer.j[0]);
    assert(outer.find_answer(&Auxiliary::come_greet, "come1") == outer.j[1]);
    assert(outer.find_answer(&Auxiliary::leave_greet, "leave0") == outer.j[0]);
    assert(outer.find_answer(&Auxiliary::leave_greet, "leave1") == outer.j[1]);
}

This concludes the complete compileable example.完整的可编译示例到此结束。

Since the number of array elements to search is variable, you should pass the structure array by pointer, not by reference.由于要搜索的数组元素的数量是可变的,因此您应该通过指针而不是引用传递结构体数组。

It also looks like you want the caller to specify which struct field to search in the array.看起来您还希望调用者指定要在数组中搜索的结构字段。 You can use a pointer-to-member to do that.您可以使用指向成员的指针来执行此操作。

Try this:尝试这个:

typedef char wordStr[100];

struct Auxiliary
{
    wordStr come_greet, leave_greet, aux_words;
};

char* search_for_val_name(Auxiliary* y, wordStr Auxiliary::*field, char* answer, int mm)
{
    for(int i = 0; i < mm; ++i)
    {
        if (strstr(answer, y[i].*field))
            return y[i].*field;
    }
    return NULL;
}
Auxiliary j[1000];
...
char *found = search_for_val_name(j, &Auxiliary::aux_words, "answer", 1000);
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM