gpt4 book ai didi

c++ - 数组不被视为容器是因为它们不是基于类的吗?

转载 作者:行者123 更新时间:2023-11-30 03:40:55 25 4
gpt4 key购买 nike

数组是容器吗?

数组在连续的内存位置中保存一组相同类型的元素,所以它们不符合容器的条件吗?

最佳答案

在标准中;与数组相关的语言功能(即 C-style int a[] 而不是 library std::array<> )与库部分中定义的容器是分开的,[container.requirements.general]/1 ;

Containers are objects that store other objects. They control allocation and deallocation of these objects through constructors, destructors, insert and erase operations.

对每种容器类型(或容器概念)的更严格要求将在后面的部分中介绍。

因此,严格来说,语言数组可能不被视为“容器”,在更非正式的意义上,它可能是。

它们确实共享某种程度的通用功能并具有一定的互换性,例如利用 std::begin() 的能力和 std::end()等等。粗略的例子是std::vector<> ,它通常被称为用作“安全”数组的容器;在以前可能使用过内置数组的地方使用。

关于c++ - 数组不被视为容器是因为它们不是基于类的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37710975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com