如何将函数中的stl列表传递为C 中的参数
How to pass the stl list in the function as parameter in C++
我有一个基类,例如:
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;
然后我将结构添加到向量:
List = new MYSTRUCT;
vector<MYSTRUCT*> SS;
SS.push_back(List);
如何将此矢量传递到娱乐函数并访问函数中的结构?
您似乎对此的含义感到困惑:
struct Mystruct
{
int a;
char *b;
}MYSTRUCT;
这是一个称为Mystruct
和的结构的声明,Mystruct
的实例称为MYSTRUCT
。因此,当您创建std::vectors
或std::lists
持有这些结构时,您需要使用 type 作为模板参数:
std::vector<Mystruct> v0; // vector holding 0 Mystructs
如果您想要持有指针的向量,则需要
std::vector<Mystruct*> v1;
这根本不会编译,因为MYSTRUCT
不是类型:
std::vector<MYSTRUCT*>
这样:
class Base
{
public:
virtual void fun(const std::vector<Mystruct*>& list) const =0;
};
class Derived:public Base
{
public:
virtual void fun(const std::vector<Mystruct*>& list)
{
//implemtation of fun
}
};
但是您的示例表明您可能会遇到其他问题,了解如何设计C 类(例如,您不需要使用此C,例如cluct定义语法,带有C )。
相关文章:
- 有没有办法捕获 STL 函数未被赋予正确参数的异常?
- C++ 如何将参数传递给 STL 函数
- C++如何获取传递给函数(STL 迭代器)的参数的名称
- 作为 Stl 容器参数的模板类
- STL 容器在函数中作为模板参数,在调用中出错
- 可以在 stl 容器上完成非类型模板参数吗?
- STL vector.insert 方法期望_InputIterator作为参数
- 模板化函数以从输入参数推断返回类型 stl-container
- IS C STL模板参数足够精确
- 如何断言模板参数的类型 STL 迭代器类型
- 为什么lambda函数用于stl函数,例如sort(),c 中的max_element()函数以两个参数为输入
- 意外参数通过使用STL数字的顺序
- 无法使用 std::function 作为参数类型(需要函数指针版本)宁愿像 STL 这样的模板,但随后它无法推断参数
- STL 按多个参数C++排序
- 模板参数适用于不同的STL容器
- 在SGI STL的实现中,向量的push_back函数不使用默认参数
- C++模板编程/STL 推导参数
- 将适当类型的迭代器作为参数返回适当类型的STL容器
- 如何将函数中的stl列表传递为C 中的参数
- 使用类作为具有正向延迟的 STL 类的模板参数