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 ()
{
/**************
1 2 3
4 5 6
***************/
// Method 1
const int ROW = 2;
const int COL = 3;
int array1[ROW][COL];
for(int i=0; i<ROW; i++)
for(int j=0; j<COL; j++)
array1[i][j] = i*COL+j+1;
// Method 2
typedef vector<vector<int> > ARRAY;
ARRAY array2;
vector<int> rowvector;
for(int i=0; i<ROW; i++)
{
rowvector.clear();
for(int j=0; j<COL; j++)
rowvector.push_back(i*COL+j+1);
array2.push_back(rowvector);
}
return 0;
}
My question is: are there other ways to define the 2D array? Which one is the most efficient one? Thanks!