multi dimensional vectors

This is a discussion on multi dimensional vectors within the C++ Programming forums, part of the General Programming Boards category; Hi i was wondering if it was possible to set a specific value in a vector. ill explain what i ...

  1. #1
    h4x0r1ng t3h m41nfr4m3!!1 r0bbb's Avatar
    Join Date
    Mar 2005
    Location
    West London
    Posts
    9

    multi dimensional vectors

    Hi i was wondering if it was possible to set a specific value in a vector. ill explain what i mean in arrays, if you could please show me the vector equivelent.

    first i declare my multidimensional array / vector

    Tile tiles[50][50]; OR vector<vector<Tile> > tiles;

    next i have a loop to loop thru each element and initialise it.

    array version:
    Code:
              for(int j = 0; j < mapHeight; j++){
                   for(int i = 0; i < mapWidth; i++){
                       tiles[i][j].init();
                   }
               }
    how would i perform this action on a multidimensional vector. baring in mind that i need to set that specific x/y value.

    i tried the above with vectors but it doesnt seam to work.

    any help will be great as i cant find much on vectors containing objects on the internet.

    rob

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The for loop is the exact same assuming mapHeight and mapWidth are correct.

    The problem might be in your constructor. Tile tiles[50][50]; is NOT the same as vector<vector<Tile> > tiles; because the vector constructor you are using creates an empty vector. If you aren't using push_back to add inner vectors, you are probably not getting the results you want.

    There are many ways to solve this, but here is the equivalent of a 50 x 50 2-D vector of default constructed Tiles:
    Code:
    vector<vector<Tile> > tiles(50, vector<Tile>(50));

  3. #3
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    I have given an example to do what you want. In my comments below, I repeatedly ask, "Why not just call init() inside the Tile constructor?" That really is the best solution, but I show how to iterate over each element anyway using standard algorithms.

    You might also consider writing a data structure to hold your vector< vector<Tile> >, so that you can make add, remove, etc. easier.

    Code:
    # include <vector>
    # include <iostream>
    # include <algorithm>
    using namespace std;
    
    class Tile {
        public:
        Tile() {
            // Why not call init here??
            init();
        }
        void init()
        {
            cout << "Init called!" << endl;
        }
    };
    
    void init_tile(Tile& t)
    
    {
        /* Why not do it in the Tile constructor? */
        t.init();
    }
    
    void init_row(vector<Tile>& row)
    
    {
        for_each(row.begin(), row.end(), init_tile);
    }
    
    int main()
    
    {
        vector< vector<Tile> > tiles;
    
        /* Add some rows */
        tiles.push_back(vector<Tile>());
        tiles.push_back(vector<Tile>());
        tiles.push_back(vector<Tile>());
    
        /* Add some tiles */
    
        tiles.at(0).push_back(Tile());
        tiles.at(1).push_back(Tile());
        tiles.at(0).push_back(Tile());
        tiles.at(2).push_back(Tile());
    
        /* Initialize here, if for some reason you can't just
            do it in the constructor... */
        for_each(tiles.begin(), tiles.end(), init_row);
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  4. #4
    h4x0r1ng t3h m41nfr4m3!!1 r0bbb's Avatar
    Join Date
    Mar 2005
    Location
    West London
    Posts
    9
    daveds method worked, thanks a lot! the reason i dont call init in the constructor is because i want to check whats returned.

    cheers for all ur help

    rob

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Code:
    for(int j = 0; j < mapHeight; j++){
                   for(int i = 0; i < mapWidth; i++){
                       tiles[i][j].init();
                   }
               }
    Code:
    unsigned long maxoffset=(mapHeight*mapWidth);
    
    for (int i=0;i<maxoffset;i++)
    {
      Tiles[i].init();
    }

    My advice. Don't use 2D vectors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 02:56 AM
  2. Problem within multi dimensional array
    By lolguy in forum C Programming
    Replies: 5
    Last Post: 12-26-2008, 07:02 AM
  3. multi dimensional array at runtime
    By sawer in forum C++ Programming
    Replies: 21
    Last Post: 05-10-2006, 10:59 PM
  4. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  5. multi dimensional array definition
    By gazsux in forum C Programming
    Replies: 2
    Last Post: 02-19-2003, 06:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21