How to define a 2D array in C++ and STL without memory manipulation?

后端 未结 8 1324
隐瞒了意图╮
隐瞒了意图╮ 2021-02-06 03:14

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 () 
{
         


        
8条回答
  •  遇见更好的自我
    2021-02-06 03:39

    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)

提交回复
热议问题