简体   繁体   English

整数数组初始化的C ++指针

[英]C++ Pointer of Array of Ints Initialization

I want to have an array accessible by all functions of a class. 我想让一个类的所有函数都可以访问一个数组。

I put the array as private variable in the header file. 我把数组作为私有变量放在头文件中。

    private:
    int* arrayName;

In the .cpp file where I implement the class, the constructor takes in an int value (size) and creates the array. 在实现类的.cpp文件中,构造函数接受一个int值(大小)并创建数组。 The goal is to fill it up 目标是填补它

ClassName::ClassName(int numElements){
  arrayName = new int[numElements]; //make arrays the size of numElements
  for(int i = 0; i<numElements; i++)
      arrayName[i] = 0;
}

I feel like this is quite inefficient. 我觉得这效率很低。 I know you can do int array[5] = {0}; 我知道你可以做int array [5] = {0}; but how do you do it when you don't initially know the size. 但是当您最初不知道尺寸时该怎么做。

If you want to zero-initialize a newed array, just do value-initialize it. 如果要对初始化的数组进行零初始化,则只需对其进行值初始化 This has the effect of zero-initializing its elements: 这具有零初始化其元素的效果:

arrayName = new int[numElements]();
//                              ^^

But you really want to be using an std::vector<int> . 但是您真的想使用std::vector<int>

private:
  std::vector<int> vname;

and

ClassName::ClassName(int numElements) : vname(numElements) {}

This way you don't have to worry about deleting an array and implementing copy constructors and assignment operators. 这样,您不必担心删除数组以及实现复制构造函数和赋值运算符。

You can use the memset function: 您可以使用memset函数:

memset(arrayName,0,sizeof(int)*numElements);

This void * memset ( void * ptr, int value, size_t num ); 这个void * memset ( void * ptr, int value, size_t num ); function sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char). 函数将ptr指向的内存块的前num个字节设置为指定值(解释为无符号字符)。

To use it you must include the string.h header file. 要使用它,您必须包括string.h头文件。

For more information: http://www.cplusplus.com/reference/cstring/memset/ 有关更多信息: http : //www.cplusplus.com/reference/cstring/memset/

What you want to do is progressively expand the array on demand. 您要做的是按需逐步扩展阵列。

 arrayName = new int[numElements];
 for(int i = 0; i<numElements; i++)
       arrayName[i] = 0;

The above code (what you gave) will give you an array of size numElements, and THEN the for loop will fill it. 上面的代码(您提供的代码)将为您提供一个大小为numElements的数组,然后for循环将填充它。 This is allocated now, and can't, as I understand it, be simply or easily resized (memset will overwrite previously held values in the array). 现在分配了它,据我所知,不能简单或轻松地调整大小(内存集将覆盖数组中以前保存的值)。

You could copy the whole array over every time you want to resize it: 您可以在每次调整大小时复制整个数组:

 int * oldarr = new int[OldSize];
 //fill your old array

 int * newarr = new int[NewSize];
 for(int i = 0; i<OldSize; i++)
       newarr[i] = oldarr[i];

Other than that, you could make the array much larger, or you could use various STLs, such as std::vector. 除此之外,您可以使数组更大,或者可以使用各种STL,例如std :: vector。 Vector can be increased with a simple push_back function, and allows [] operator access (like arr[5] and whatnot). 向量可以通过简单的push_back函数来增加,并允许[]运算符访问(如arr [5]和诸如此类)。

Hope this helps! 希望这可以帮助!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM