如何在c++中传递结构向量给函数
how to pass a structure vector to a function in C++?
在下面的示例代码中,我需要将结构向量传递给函数。
class A {
public:
struct mystruct {
mystruct (int _label, double _dist) : label(_label), dist(_dist) {}
int label;
double dist;
};
}
我如下声明了vector:
vector<A:: mystruct > mystry;
现在在类"A"中有一个函数如下:
myfunc ( vector<mystruct> &mystry );
如何将结构向量传递给我的"myfunc"?
试试这个
#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;
}
首先,您需要创建一个A
的实例,如下所示:
A a;
然后,您需要在a
上调用myfunc
,将值mystry
传递给它。
a.myfunc(mystry);
相关文章:
- 将结构向量排序为子组
- NLOHMANN 的 JSON 库将数组转换为结构向量
- 如何在 c++ 中创建结构向量的映射
- 带unique_ptr的结构向量
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 将信息输入到下面显示的结构向量中的正确语法/格式是什么
- C++对结构向量进行排序
- 在C++中对结构向量进行排序时出现问题
- 将结构向量保存到文件中,并从C++文件中读取结构向量
- 搜索并替换结构向量中的值
- 在结构向量中查找最接近的值
- 清除结构向量时会发生什么?
- 如何从 node-ffi 调用 c++ 中以结构向量作为参数的方法?
- 对结构向量进行两次排序
- 将一种数据类型的向量复制到同一数据类型的结构向量中的有效方法是什么
- 如何二进制搜索结构向量并在适当的索引处插入
- 结构向量中自定义结构函数的内存使用
- C++11-在结构向量中修改结构的成员
- 我正在尝试找到一个特定的结构成员,该成员是使用 lambda 函数的结构向量
- 在结构向量中搜索一个数据成员,然后打印匹配的所有数据成员