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

后端 未结 8 1325
隐瞒了意图╮
隐瞒了意图╮ 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:42

    If you know the elements beforehand then you could just do

    int arr[2][3] = {{1,2, 3}, {4, 5, 6}};

    This should be more efficient than method1 and method2. Using vectors, you are not doing memory manipulation yourself, but the vector implementation will probably use a dynamically allocated array.

提交回复
热议问题