简体   繁体   English

如何在运行时定义成员数组大小

[英]How to define member array size at runtime

Let's say I have a class that has a member which is an array. 假设我有一个具有数组成员的类。 Is it possible to define its size upon construction/at run-time, in the following way: 是否可以在构建时/运行时定义其大小,方式如下:

class myClass {
    private:
        int myArray[n]
    public:
        myClass();
        someOtherMethod();
};

Where n is a variable that is defined based on user input. 其中n是基于用户输入定义的变量。 If not, what would be the best alternative? 如果没有,最好的选择是什么?

It depends. 这取决于。

Semantically, there are 3 types of arrays: 从语义上讲,有三种类型的数组:

  • arrays with a size fixed at compile time 在编译时固定大小的数组
  • arrays with a size fixed at runtime 大小在运行时固定的数组
  • arrays with a dynamic size 具有动态大小的数组

C++ directly supports the first and third cases, respectively with regular arrays and the std::vector class. C ++直接支持第一种和第三种情况,分别使用常规数组和std::vector类。

C also supports the second type with two constructs: C还支持带有两个结构的第二种类型:

  • variable length arrays (on the stack) 可变长度数组(在堆栈上)
  • the oldie struct hack or tail-padding oldie struct hack或tail-padding

I would advise, in C++, using the std::vector class in your case. 我会建议,在C ++中,在你的情况下使用std::vector类。 It provides more than what you need, but is simpler to use. 它提供的不仅仅是您需要的,而且使用起来更简单。

On the other hand, you can still use tail-padding, even in C++. 另一方面,即使在C ++中,您仍然可以使用尾部填充。 It does require careful engineering though. 它确实需要仔细的工程。

Use a vector. 使用矢量。

class myClass {
    private:
        std::vector<int> myArray;
    public:
        myClass();
        someOtherMethod();
};

myClass::myClass (int size)
    : myArray (size)
{ 
    ...
}

Then, you can fill in the vector as you would an array. 然后,您可以像填充数组一样填充矢量。 Alternatively, as Nawaz points out, use reserve() , which reserves space for new elements, and/or push_back() , which adds elements onto the back, one at a time. 或者,正如Nawaz指出的那样,使用为新元素保留空间的reserve()和/或push_back() ,它一次一个地添加元素到背面。

The class template std::vector is designed for this purpose. 类模板std::vector就是为此目的而设计的。

class myClass {
    private:
        std::vector<int> myArray;
    public:
        myClass(int size);
        someOtherMethod();
};

myClass::myClass(int size) : myArray(size)
{
}

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

相关问题 如何定义作为不可实例化类的静态成员的数组的大小? - How to define the size of an array that is a static member of a non instanciable class? 如何在类的构造函数中定义成员向量的大小? - How to define the size of member vector in constructor of a class? C++:如何创建在运行时确定大小的成员二维数组 - C++: How to create a member 2D-array of size determined at runtime 如何将固定大小的boost循环缓冲区定义为class成员? - how to define fixed size boost circular buffer as class member? 如何使用 const 成员变量设置数组大小? - How to set array size with const member variable? 如何在Cython中使用#define作为char数组的大小 - How to use #define as size of char array in Cython 如何在运行时增加结构数组的大小 - How to increase struct array size at runtime 未知大小的数组作为类成员,用于在运行时创建数组对象(对象创建时) - array of unknown size as class member for making objects of array at runtime(object creating time) 如何为模板化类的成员函数定义静态数组? - How do I define a static array to member functions of a templated class? 初始化在类默认ctor类型的#define中定义的非静态成员数组 - Initializing non-static member array of size defined in a #define of class type without default ctor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM