Is it possible to have an array of vectors, or a vector of vectors?
This would be in place of a 2d array.
Is it possible to have an array of vectors, or a vector of vectors?
This would be in place of a 2d array.
Yes.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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];
Last edited by earth_angel; 07-12-2005 at 07:15 AM.
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; }
Free code: http://sol-biotech.com/code/.
It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up
The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
What is the proper way of creating one dynamically?
The usual, e.g.
But yeah, a vector of vectors might be better.Code:std::vector<int>* foo; foo = new std::vector<int>[size]; //use foo for some devious purpose delete[] foo;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
OK.
That's intense, took me a while to figure out all the counters and such.
Looking at that example, I tried this code :I know it's wrong, how would it be fixed?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); } }
> The usual, e.g.
Oh, It's always easier then I think. I'll give it a shot
There's no reason to dynamically allocate an array of vectors. Try giving a vector of vectors a shot.