是 std:vector<char[30]>有效,或者如何实现这一目标?(固定大小数组的向量?) [英] is std:vector<char[30]> valid, or how to achieve this? (vector of fixed sized array?)
问题描述
我想要一个固定一维的二维数组的概念.
I would like to have a notion of 2d arrays with one dimension fixed.
类似于不断增长的长度为 30 的字符数组列表.
something like a growing list of char arrays of length 30.
我可以做类似矢量的事情吗这是有效的,还是有办法实现这一点
Can i do something like vector is that valid, or is there a way of achieving this
推荐答案
否:存储在 vector
(或任何其他标准库容器)中的类型必须是可复制和可分配的.数组两者都不是.
No: the type stored in a vector
(or any other Standard Library container) must be both copyable and assignable. An array is neither.
您可以(并且应该)使用 std::array
(或者,如果您的实现不支持,则使用 boost::array
).array
类模板为普通数组提供了一个非常轻量级的类似容器的包装器;在大多数情况下,它可以像普通数组一样使用,并且开销为零(具有良好的实现和编译器优化打开).
You can (and should) use std::array
(or, if your implementation doesn't support that, boost::array
). The array
class template provides a very lightweight container-like wrapper around an ordinary array; it can be used just like an ordinary array in most circumstances and has zero overhead (with a good implementation and with compiler optimizations turned on).
当您可以使用 array
类模板代替时,确实没有充分的理由使用普通数组(如 char[30]
).
There is really no good reason to use an ordinary array (like char[30]
) when you can use the array
class template instead.
这篇关于是 std:vector<char[30]>有效,或者如何实现这一目标?(固定大小数组的向量?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!