is std:vector<char[30]> valid, or how to achieve this? (vector of fixed sized array?) [closed]
I would like to have a notion of 2d arrays with one dimension fixed.
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
No: the type stored in a vector
(or any other Standard Library container) must be both copyable and assignable. An array is neither.
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).
There is really no good reason to use an ordinary array (like char[30]
) when you can use the array
class template instead.
A std::vector of std::array sounds like a better idea.
精彩评论