简体   繁体   English

有没有办法在 C++ 中静态初始化动态分配的数组?

[英]Is there a way to statically-initialize a dynamically-allocated array in C++?

In C++, I can statically initialize an array, eg:在 C++ 中,我可以静态初始化一个数组,例如:

int a[] = { 1, 2, 3 };

Is there an easy way to initialize a dynamically-allocated array to a set of immediate values?有没有一种简单的方法可以将动态分配的数组初始化为一组立即值?

int *p = new int[3];
p = { 1, 2, 3 }; // syntax error

...or do I absolutely have to copy these values manually? ...还是我绝对必须手动复制这些值?

You can in C++0x:您可以在 C++0x 中:

int* p = new int[3] { 1, 2, 3 };
...
delete[] p;

But I like vectors better:但我更喜欢向量:

std::vector<int> v { 1, 2, 3 };

If you don't have a C++0x compiler, boost can help you:如果您没有 C++0x 编译器,boost 可以帮助您:

#include <boost/assign/list_of.hpp>
using boost::assign::list_of;

vector<int> v = list_of(1)(2)(3);

You have to assign each element of the dynamic array explicitly (eg in a for or while loop)您必须显式分配动态数组的每个元素(例如在 for 或 while 循环中)

However the syntax int *p = new int [3]();但是语法int *p = new int [3](); does initialize all elements to 0 (value initialization $8.5/5)确实将所有元素初始化为 0(值初始化 $8.5/5)

To avoid endless push_backs, I usually initialize a tr1::array and create a std::vector (or any other container std container) out of the result;为了避免无休止的 push_back,我通常会初始化一个tr1::array并从结果中创建一个std::vector (或任何其他容器 std 容器);

const std::tr1::array<T, 6> values = {T(1), T(2), T(3), T(4), T(5), T(6)};
std::vector <T> vec(values.begin(), values.end());

The only annoyance here is that you have to provide the number of values explicitly.这里唯一的烦恼是您必须明确提供值的数量。

This can of course be done without using a tr1::array aswell;这当然可以在不使用tr1::array情况下完成;

const T values[] = {T(1), T(2), T(3), T(4), T(5), T(6)};
std::vector <T> vec(&values[0], &values[sizeof(values)/sizeof(values[0])]);

Althrough you dont have to provide the number of elements explicitly, I prefer the first version.虽然您不必明确提供元素的数量,但我更喜欢第一个版本。

No, you cannot initialize a dynamically created array in the same way.不,您不能以相同的方式初始化动态创建的数组。

Most of the time you'll find yourself using dynamic allocation in situations where static initialization doesn't really make sense anyway.大多数情况下,您会发现自己在静态初始化没有任何意义的情况下使用动态分配。 Such as when you have arrays containing thousands of items.例如,当您有包含数千个项目的数组时。 So this isn't usually a big deal.所以这通常不是什么大问题。

Using helper variable:使用辅助变量:

const int p_data[] = {1, 2, 3};
int* p = (int*)memcpy(new int[3], p_data, sizeof(p_data));

or, one line或者,一行

int p_data[] = {1, 2, 3},  *p = (int*)memcpy(new int[3], p_data, sizeof(p_data));

Never heard of such thing possible, that would be nice to have.从来没有听说过这样的事情可能,那会很好。

Keep in mind that by initializing the array in the code that way请记住,通过以这种方式在代码中初始化数组

int a[] = { 1, 2, 3 };

..... only gains you easier code writing and NOT performance . ..... 只会让您更轻松地编写代码,而不是性能 After all, the CPU will do the work of assigning values to the array, either way you do it.毕竟,无论您采用哪种方式,CPU 都会完成为数组赋值的工作。

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

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