Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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+中的函数+;?_C++_Oop - Fatal编程技术网

C++ 如何将结构向量传递给C+中的函数+;?

C++ 如何将结构向量传递给C+中的函数+;?,c++,oop,C++,Oop,在下面的示例代码中,我需要将结构向量传递给函数 class A { public: struct mystruct { mystruct (int _label, double _dist) : label(_label), dist(_dist) {} int label; double dist; }; } 我声明向量如下: vector<A:: mystruct > mystry; vectorm

在下面的示例代码中,我需要将结构向量传递给函数

class A {
public:
    struct mystruct {   
        mystruct (int _label, double _dist) : label(_label), dist(_dist) {}   
        int label;
        double dist;
    };
}
我声明向量如下:

 vector<A:: mystruct > mystry;
vectormystry;
在这个类“A”中有一个函数,如下所示

  myfunc ( vector<mystruct> &mystry );
myfunc(vector&mystry);

如何将结构向量传递给我的“myfunc”

首先,您需要创建一个
A
的实例,如下所示:

A a;
然后,您需要在
a
上调用
myfunc
,将值
mystry
传递给它

a.myfunc(mystry);

首先,您需要创建
A
的实例,如下所示:

A a;
然后,您需要在
a
上调用
myfunc
,将值
mystry
传递给它

a.myfunc(mystry);
试试这个

#include <iostream>
#include <vector>

using namespace std;

class A {
public:
    struct mystruct {   
        mystruct (int _label, double _dist) : label(_label), dist(_dist) {}   
        int label;
        double dist;
    };

    void myfunc ( vector<mystruct> &mystry ){
        cout << mystry[0].label <<endl;
        cout << mystry[0].dist <<endl;
    }
};

int main(){
    A::mystruct temp_mystruct(5,2.5); \\create instance of struct.

    vector<A:: mystruct > mystry; \\ create vector of struct 
    mystry.push_back(temp_mystruct); \\ add struct instance to vector

    A a; \\ create instance of the class
    a.myfunc(mystry); \\call function
    system("pause");
    return 0;
}
#包括
#包括
使用名称空间std;
甲级{
公众:
结构mystruct{
mystruct(int _-label,double _-dist):label(_-label),dist(_-dist){
int标签;
双区;
};
void myfunc(向量和mystry){
试试这个

#include <iostream>
#include <vector>

using namespace std;

class A {
public:
    struct mystruct {   
        mystruct (int _label, double _dist) : label(_label), dist(_dist) {}   
        int label;
        double dist;
    };

    void myfunc ( vector<mystruct> &mystry ){
        cout << mystry[0].label <<endl;
        cout << mystry[0].dist <<endl;
    }
};

int main(){
    A::mystruct temp_mystruct(5,2.5); \\create instance of struct.

    vector<A:: mystruct > mystry; \\ create vector of struct 
    mystry.push_back(temp_mystruct); \\ add struct instance to vector

    A a; \\ create instance of the class
    a.myfunc(mystry); \\call function
    system("pause");
    return 0;
}
#包括
#包括
使用名称空间std;
甲级{
公众:
结构mystruct{
mystruct(int _-label,double _-dist):label(_-label),dist(_-dist){
int标签;
双区;
};
void myfunc(向量和mystry){

cout@nommyravian这个问题是关于传递一个向量,在我的例子中是关于向量的结构向量?这是关于向量的问题。它们包含什么并不重要。你哪里有问题?将向量传递给函数就像将其他任何东西传递给函数一样。@nommyravian这个问题是关于传递一个向量,在我的例子中是它的结构向量?这是关于向量的问题。向量包含什么并不重要。你哪里有问题?将向量传递给函数就像将其他任何东西传递给函数一样。这似乎过于复杂。OP只询问如何将向量传递给
myfunc
@MichaelDorst-1。这是一个工作示例,因此OP可以调试和更新我没有想到。2.我评论了OP需要理解并使程序易懂的要点。3.我在这个简单的程序中没有看到任何完整的内容,这些内容大多是从OPs代码中复制的。=)这似乎太复杂了。OP只问了如何将向量传递到
myfunc
@MichaelDorst-1。这是一个工作示例,因此P可以调试并获得想法。2.我评论了OP需要理解并使程序易于理解的要点。3.我在这个简单的程序中没有看到任何完整的内容,这些内容大多是从OPs代码中复制的。=)