有什么用于unique_ptr与数组? [英] Is there any use for unique_ptr with array?

查看:166
本文介绍了有什么用于unique_ptr与数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std :: unique_ptr 支持数组,例如:

std::unique_ptr<int[]> p(new int[10]);

但是需要吗?可能使用 std :: vector std :: array 更方便。

but is it needed? probably it is more convenient to use std::vector or std::array.

您是否找到该结构的任何用途?

Do you find any use for that construct?

推荐答案

有些人没有奢侈使用 std :: vector ,甚至使用分配器。有些人需要一个动态大小的数组,所以 std :: array 是。有些人从已知的返回数组的其他代码获取它们的数组;并且该代码不会被重写以返回向量或某事。

Some people do not have the luxury of using std::vector, even with allocators. Some people need a dynamically sized array, so std::array is out. And some people get their arrays from other code that is known to return an array; and that code isn't going to be rewritten to return a vector or something.

通过允许<$ c

简而言之,您使用 unique_ptr< ; T []> 当你需要时。当替代品根本不能为你工作。这是最后手段。

In short, you use unique_ptr<T[]> when you need to. When the alternatives simply aren't going to work for you. It's a tool of last resort.

这篇关于有什么用于unique_ptr与数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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