如何获取存储在unique_ptr中的数组大小? [英] How to get array size stored in unique_ptr?

查看:357
本文介绍了如何获取存储在unique_ptr中的数组大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我这样做:

std::unique_ptr<int[]> uparr(new int[1984]);

,而我将 uparr 传递给某人却没有通过对他们来说, 1984 可以看到它们有多少个元素吗?

aka数组的unique_ptr是否等效于vector的.size()?

and I pass uparr to somebody without passing the 1984 to them can they see how many elements it has?
aka is there equivalent of vector's .size() for unique_ptr of array ?

推荐答案

不,没有。动态数组是一种有缺陷的语言功能。本质上,您总是希望/需要分别传递数组长度(除非您有某种定点策略)。因此,您最好使用 std :: vector (如果您不介意额外的容量)。

No, there isn't. Dynamic arrays are a somewhat defective language feature. You'll essentially always want/need to pass the array length around separately (unless you have some kind of sentinel policy). So you might as well use std::vector (if you don't mind the extra word for the capacity).

这篇关于如何获取存储在unique_ptr中的数组大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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