1

Its been just 15 days I am coding in c++, and I have encountered something where I am stuck. I need to use a dynamic two dimensional array to suffice my purpose. I use std::vector to create one dimensional dynamic array and I have no clue how a two dimensional dynamic array is created. I am using VC++ if it helps but I do not VC++ library functions.

Does anyone know how to get it done?

vin
  • 869
  • 4
  • 17
  • 33

6 Answers6

3

You can do it with nested vectors:

int number_of_rows, number_of_columns;

std::vector < std::vector <int> > array;

array.resize(number_of_rows);

for (int i=0; i < number_of_rows; ++i)
    array[i].resize(number_of_columns);

array[1][2] = 3; // e.g. of assignment

Initializing the complete array size in the constructor:

std::vector <std::vector <int> > (nor, std::vector<int>(noc));

Note however that a nested std::vector structure does not bind you to a two dimensional array -- every one of the nested vectors can have a different size. E.g.:

std::vector <std::vector <int> > not_array(3); 
    // contains 3 inner empty vectors
for (int i=0; i < 3; ++i)
    arrray[i].resize(i+1);

Now the not_array contains vectors of sizes 1, 2 and 3 in that order.

If you want to be sure you are using a matrix (2D), not just a vector of (1D) vectors, you can use an implementation from an outside library.

  • OpenCV is a computer vision library, and contains class cv::Mat
  • boost is a general-purpose library and has an implementation of matrix class
penelope
  • 8,251
  • 8
  • 45
  • 87
1

If your array dimensions can be variable (e.g. each row can have different length), vector<vector<int> > is a good solution (replace int with your specific element type).

You can use this approach even if the length of each rows are the same, but it might be more work to ensure this property. In the latter case you might be better off finding a library that has support for some Matrix type.

Attila
  • 28,265
  • 3
  • 46
  • 55
1

There is no 2-dimensionnal data structure in the C++/STL standard library.

As others said, you could do the trick by nesting STL containers such as vector. But it won't guaranty the consistency of the overall structure. (two lines of the matrix wouldn't be guaranteed to be the same length).

You should encapsulate this inside an object to enforce a correct matrix structure. Or you could use an already existing Matrix data structure, I'm sure there's a lot of libraries which supplies it.

zakinster
  • 10,508
  • 1
  • 41
  • 52
0

A two-dimensional array is a vector of vectors. See for example.

// Declare size of two dimensional array and initialize.
vector< vector<int> > vI2Matrix(3, vector<int>(2,0));   
vI2Matrix[0][0] = 0;
vI2Matrix[0][1] = 1;
vI2Matrix[1][0] = 10;
vI2Matrix[1][1] = 11;
vI2Matrix[2][0] = 20;
vI2Matrix[2][1] = 21;
cout << "Loop by index:" << endl;

int ii, jj;
for(ii=0; ii < 3; ii++)
{
  for(jj=0; jj < 2; jj++)
  {
     cout << vI2Matrix[ii][jj] << endl;
  }
}
Jacob Seleznev
  • 8,013
  • 3
  • 24
  • 34
0

If you are dealing with scalar arrays, you should definitely consider using Eigen.

Eigen is a C++ template library for linear algebra: matrices, vectors, numerical solvers, and related algorithms.

It comes with a great documentation which provides numerous examples.

Resizing arrays on Eigen is straightforward:

#include <Eigen/Dense>
using namespace Eigen;
...
MatrixXd m(5,5);  // 5x5 matrix of double
m.resize(10,5);   // resizing to a 10x5 matrix

Note that Eigen is also extensible to custom scalar types.

BenC
  • 8,729
  • 3
  • 49
  • 68
-1
std::vector< std::vector<int> >

later

Enlarging std::vector quite simple but reducing has some issues. See refs.

When you are reducing the size you are deleting extra elements only logically (physically reducing of memory is not guaranteed ). To force reducing memory you can use such trick.

Community
  • 1
  • 1
triclosan
  • 5,578
  • 6
  • 26
  • 50
  • fastest-gun-in-the-west answer just to get an upvote! ugly! evil! mean! – penelope Jun 06 '12 at 12:14
  • Go make greatest shot if you can, @penelope ! – triclosan Jun 06 '12 at 12:16
  • @penelope, what's wrong with the answer? You wanted to say this yourself? – Forgottn Jun 06 '12 at 12:17
  • @vin `std::vector` is template-based container its can be parametrized by POD or more complicated data-types – triclosan Jun 06 '12 at 12:18
  • @vin, totally. It works. But better use a typedef: `typedef std::vector int_array;` and `typedef std::vector int_matrix;` – Forgottn Jun 06 '12 at 12:19
  • @Forgottin writting half a line of code is not an answer. It's not educative, it's not helpful. It does not explain how to use it, or give any arguments. Every such answer I have ever seen was a low quality answer posted with sole purpose of being listed as the first and collecting quick reputation – penelope Jun 06 '12 at 12:20
  • 1
    @penelope, it's not Wikipedia about programming in here. It's questions and answers. A question is how to make an array of arrays out of `vector` assumes, that a man asking this knows what the `vector` is. My point of view is that the answer can be extened as a result of comment. But nevertheless. This is *correct* answer. *Short*, but *correct*. And downvote it to say the same, but in many sentences is bad idea. – Forgottn Jun 06 '12 at 12:25
  • @ triclosan what I need to do is resize both the dimensions of the array what would the syntax be.. like I can set values by doing something of this sort array.at(i).at(j), but what if I have to resize i &j how is that done? – vin Jun 06 '12 at 12:28
  • @vin, then write you own class, that encapsulates the vector of vectors and ensures that they'll always look like matrix. Or find a library implementing a Matrix class. – Forgottn Jun 06 '12 at 12:31
  • @Forgottin Not correct in the strictest sense of the word... Array has each row of the same length. This implementation does not guarantee it. If the programmer is careful, he can of course make it work, but without that elaboration, since this is not **an array** of ints, it is not correct. – penelope Jun 06 '12 at 12:31
  • @penelope C++ gives you many ways to shoot in your leg. You should always be careful. If somebody does not now how really the `vector` class is implemented, that always bad idea to teach him for any abstractions. He's code monkey, not a programmer. Good idea is to start with books/articles and documentation to the specific compiler. – Forgottn Jun 06 '12 at 12:36