简体   繁体   English

有没有更好的方法在 C++ 中初始化分配的数组?

[英]Is there a better way to initialize an allocated array in C++?

How to write this in another (perhaps shorter) way?如何以另一种(可能更短)的方式写这个? Is there a better way to initialize an allocated array in C++?有没有更好的方法在 C++ 中初始化分配的数组?

int main(void) {
   int* a;
   a = new int[10];
   for (int i=0; i < 10; ++i) a[i] = 0;
}
 int *a =new int[10](); // Value initialization

ISO C++ Section 8.5/5

To value-initialize an object of type T means:对 T 类型的对象进行值初始化意味着:

— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor); — 如果 T 是具有用户声明的构造函数 (12.1) 的类类型(第 9 条),则调用 T 的默认构造函数(如果 T 没有可访问的默认构造函数,则初始化是格式错误的);

— if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized; — 如果 T 是没有用户声明的构造函数的非联合类类型,则 T 的每个非静态数据成员和基类组件都进行了值初始化;

— if T is an array type, then each element is value-initialized; — 如果 T 是数组类型,则每个元素都进行了值初始化;

otherwise, the object is zero-initialized 否则,对象是零初始化的

For differences between the terms zero initialization , value initialization and default initialization , read this有关术语zero initializationvalue initializationdefault initialization之间的差异,请阅读此内容

std::vector<int> vec(10, 0); 
int *a = &vec.front();

How about 3 ways? 3种方式怎么样?

1.    int *a = new int[10]();

2.    std::vector<int> a(10, 0);

3.    int *a = new int[10];
      memset(a, 0, sizeof(int) * 10);

Due to popular demand, a couple more:由于大众需求,还有几个:

4.    int *a = new int[10];
      std::fill(a, a + 10, 0);

5.    std::vector<int> a(10);
      std::fill(a.begin(), a.end(), 0);

You could use memset你可以使用memset

Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).将 ptr 指向的内存块的前 num 个字节设置为指定值(解释为无符号字符)。

int main(void) { int *a; a = new int[10]; for(int i=0;i<10;++i) a[i]=0; }

;-)

#include <algorithm>

int main() {
    int *a = new int[10];
    std::fill(a, a + 10, 0);
}
int *a = (int*) calloc(10, sizeof(*a));

(并检查是否为 NULL,或针对 calloc 重写安全包装器)。

Maybe you could try something like this:也许你可以尝试这样的事情:

int* initIntArray(int size) {
    int *temp = new int[size];
    for(int i = 0; i < size; i++) {
        temp[i]=0;
    }
    return temp;
}

int main () {
    int* a = initIntArray(10);
    int* b = initIntArray(10);
    int* c = initIntArray(10);

    //do stuff with arrays

    delete [] a;
    delete [] b;
    delete [] c;

    return 0;
}

by the way, what about using calloc()?顺便说一下,使用 calloc() 怎么样? say

int i*=(int[10])calloc(10*sizeof(int))

well i'm just another C guy.. any comment is welcomed here好吧,我只是另一个 C 人.. 在这里欢迎任何评论

I'm a C guy and not too sure what "new" really does, but could this work?我是一个 C 人,不太确定“新”到底是做什么的,但这可以工作吗?


int
main( void ) {
   int i = 10;              // start at the far end of the array
   int *a = new int[10]; 
   while ( i-- ) a[i] = 0;  // while ( i == 9, 8, 7, ... , 0 )
}

Just to show off my new loop-counter favorite: while(condition).只是为了炫耀我最喜欢的新循环计数器:while(condition)。

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

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