gpt4 book ai didi

c++ - 如何在 C++ 中将函数中的 STL 列表作为参数传递

转载 作者:行者123 更新时间:2023-11-28 03:28:54 26 4
gpt4 key购买 nike

我有一个基类:

class Base
{
public:
virtual void fun() const =0;
};

class Derived: public Base
{
virtual void fun()
{
//implemtation of fun
}
};

我有一个全局结构:

struct Mystruct {
int a;
char *b;
} MYSTRUCT;

然后我将结构添加到 vector 中:

List  = new MYSTRUCT;
vector<MYSTRUCT*> SS;
SS.push_back(List);

如何将 this vector 传递给 fun 函数并访问函数中的结构?

最佳答案

你似乎对这个的意思感到困惑:

struct Mystruct
{
int a;
char *b;
}MYSTRUCT;

这是一个名为Mystruct 的结构的声明一个名为MYSTRUCTMystruct 实例。因此,当您创建 std::vectorsstd::lists 来保存这些结构时,您需要使用 type 作为模板参数:

std::vector<Mystruct> v0; // vector holding 0 Mystructs

如果你想要一个包含指针的 vector ,你需要

std::vector<Mystruct*> v1;

这将无法编译,因为 MYSTRUCT 不是类型:

std::vector<MYSTRUCT*>

关于c++ - 如何在 C++ 中将函数中的 STL 列表作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13146209/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com