Thread: Array of vectors

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    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.

  4. #4
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    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

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    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?

  7. #7
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    > The usual, e.g.

    Oh, It's always easier then I think. I'll give it a shot

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There's no reason to dynamically allocate an array of vectors. Try giving a vector of vectors a shot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  4. Arrays vs Vectors
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2006, 02:06 AM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM