There are several ways to define a 2D array in C++ and STL without memory manipulation, and the following codes illustrate two different methods:
int main ()
{
To declare a 2D array using std::vector
, you can use this kind of construction:
vector > matrix( n, vector(m, -1) );
This creates a 2D array matrix
of size n
by m
, with all elements initialised to -1
.
It's basically a nesting of the "initialise with n
items of value val
" constructor:
vector (size_type n, const value_type& val,
const allocator_type& alloc = allocator_type());
(constructor definition copied from here)