C++ 如何声明没有特定大小的数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17259098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:02:32  来源:igfitidea点击:

How to declare an array without specific size?

c++arraysconstructor

提问by Shelef

How can I declare an array without specific size as a class member? I want to set the size of this array immediately in the class constructor. Is it possible to do such thing without using the heap or without resizing the array?

如何将没有特定大小的数组声明为类成员?我想在类构造函数中立即设置这个数组的大小。是否可以在不使用堆或不调整数组大小的情况下执行此类操作?

回答by Alok Save

Variable length arrays are not allowed by C++ standard. The options you have are:

C++ 标准不允许变长数组。您拥有的选项是:

  • Use a std::vectoror
  • Use a pointer to dynamic memory
  • 使用一个std::vector
  • 使用指向动态内存的指针

Note that Variable length arrays are supported by most compilers as a extension, So if you are not worried of portability and your compiler supports it, You can use it. ofcourse it has its own share of problems but its a option given the constraints you cited.

请注意,大多数编译器都支持可变长度数组作为扩展,因此如果您不担心可移植性并且您的编译器支持它,则可以使用它。当然,它有自己的问题,但鉴于您引用的限制,它是一个选项。

回答by juanchopanza

C++ requires the size of an automatic storage array to be known at compile time, otherwise the array must be dynamically allocated. So you would need dynamic allocation at some level, but you don't have to concern yourself with doing it directly: just use an std::vector:

C++ 要求在编译时知道自动存储数组的大小,否则必须动态分配数组。因此,您需要在某个级别进行动态分配,但您不必担心直接执行此操作:只需使用std::vector:

#include <vector>

class Foo
{
 public:
  Foo() : v_(5) {}
 private:
  std::vector<int> v_;
};

Here, v_is a vector holding ints, and is constructed to have size 5. The vector takes care of dynamic allocation for you.

这里,v_是一个持有 的向量ints,并被构造为具有大小5。该向量负责为您进行动态分配。

In C++14, you will have the option of using std::dynarray, which is very much like an std::vector, except that its size is fixed at construction. This has a closer match to the plain dynamically allocated array functionality.

在 C++14 中,您可以选择使用std::dynarray,它与 非常相似std::vector,只是它的大小在构造时是固定的。这与普通的动态分配数组功能更接近。

回答by Taimour

You can use a vector, by including header file #include<vector>It can grow and shrink in size as required and vectors have built in methods/functions which can make your work easy.

您可以通过包含头文件来使用向量。#include<vector>它可以根据需要增大和缩小大小,并且向量具有内置的方法/函数,可以使您的工作变得轻松。

回答by AnT

Class member array have to be declared with exact compile-time size. There's no way around it.

类成员数组必须以精确的编译时大小声明。没有办法解决。

The only way you can declare an array as an immediate member of the class and yet be able to decide its size at run time would be the popular "struct hack"technique inherited from C.

您可以将数组声明为类的直接成员,并且能够在运行时决定其大小的唯一方法是流行的从 C 继承的“结构黑客”技术。

Other than that, you have to declare your array as an indirect member of the class: either declare a member of pointer type and allocate memory later, or use some library implementation of run-time sized array (like std::vector)

除此之外,您必须将数组声明为类的间接成员:要么声明指针类型的成员并稍后分配内存,要么使用运行时大小数组的一些库实现(如std::vector