# Array of vectors

• 07-12-2005
earth_angel
Array of vectors
Is it possible to have an array of vectors, or a vector of vectors?
This would be in place of a 2d array.
• 07-12-2005
laserlight
Yes.
• 07-12-2005
earth_angel
I tried creating it like this but it doesn't work. What is the proper way of creating one dynamically?
Code:

```k = vars.NumScan; vector <long int> vectAWords[k];```
• 07-12-2005
mitakeet
Here is an example of a 3d array of vectors emulating a matrix using references to avoid copying:

Code:

```#include <iostream> #include <vector> using namespace std; int main(){     int x = 3, y = 3, z = 3;     int i, j, k;     vector < vector < vector < int > > > matrix3d;     matrix3d.resize(x);     for (i=0; i<x; i++){         vector < vector < int > > &vect2d = matrix3d[i];         vect2d.resize(y);         for (j=0; j<y; j++){             vector < int > &vectLastD = vect2d[j];             vectLastD.resize(z);             for (k=0; k<z; k++){                 vectLastD[k] = i+j+k;             }         }     }     for (i=0; i<x; i++){         for (j=0; j<y; j++){             for (k=0; k<z; k++){                 cout << matrix3d[i][j][k] << ' ';             }             cout << endl;         }         cout << endl;     }     return 0; }```
• 07-12-2005
laserlight
What is the proper way of creating one dynamically?
The usual, e.g.
Code:

```std::vector<int>* foo; foo = new std::vector<int>[size]; //use foo for some devious purpose delete[] foo;```
But yeah, a vector of vectors might be better.
• 07-12-2005
earth_angel
OK.
That's intense, took me a while to figure out all the counters and such.

Looking at that example, I tried this code :
Code:

``` int TempWord = 0;         long int *AWordMtx;         AWords = new long int[vars.NumScan];         for (j=0; j< vars.NumScan; j++)         {                 vector <long int> AWordLine = AWordMtx[j];                 for( i=0; i< vars.vectParmSelected.size(); i++)                 {                         TempValue = i+2;                         if(TempValue > 1)                                 AWordMtx[i].AWordLine.push_back(TempWord);                 }         }```
I know it's wrong, how would it be fixed?
• 07-12-2005
earth_angel
> The usual, e.g.

Oh, It's always easier then I think. I'll give it a shot
• 07-12-2005
Daved
There's no reason to dynamically allocate an array of vectors. Try giving a vector of vectors a shot.