是否有可能通过使用new运算符初始化在C ++ 11的阵列 [英] Is it possible to initialise an array in C++ 11 by using new operator

查看:117
本文介绍了是否有可能通过使用new运算符初始化在C ++ 11的阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我想初始化C中的数组++这样的

Hi everyone i want to initialize an array in C++ like this

int数组[10] = {} 1,2,3,4,5,6,7,8,9,10;

但我使用new运算符。

but i am using the new operator.

我知道我能做到像下面,然后遍历和分配值

I know i can do like below and then iterate and assign the values

的shared_ptr< INT [] GT; L(新INT [7]);

但我更喜欢,如果有某种方式,我可以在新的命令在初始化

but i would really love if there is some way in which i could initialize it during the new command

像这样的shared_ptr< INT [] GT; L(新INT [7] = {1,2,3,4,5,6,7});
但可悲的是,这不是一个有效的语法。

something like this shared_ptr<int[]> l (new int[7] ={1,2,3,4,5,6,7}); but sadly this is not a valid syntax.

此外,在C ++ 11标准的一个新的STL容器添加阵列,可以有人请是否使用正常的阵列或STL阵列可以做到这一点无论是告诉我

Also in C++ 11 standard a new STL container Array is added,can someone please tell me whether either using a normal array or the STL Array this can be achieved

推荐答案

C ++ 11提供了一个容器 initializer_list 它的工作原理是这样的:

C++11 gives containers an initializer_list which works like this:

std::vector<int> array = {1,2,3,4,5};

矢量的动态数组类。

这是你的shared_ptr的版本:

Here's your shared_ptr version:

std::shared_ptr<int> ptr(new int[5]{1,2,3,4,5}, std::default_delete<int[]>());

这篇关于是否有可能通过使用new运算符初始化在C ++ 11的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆