Like Tree1Likes
  • 1 Post By VirtualAce

initializing a vector of structs in class constructor

This is a discussion on initializing a vector of structs in class constructor within the C++ Programming forums, part of the General Programming Boards category; How would I initialize certain fields in a struct in a class constructor? I want to create a 11x11 vector. ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    18

    initializing a vector of structs in class constructor

    How would I initialize certain fields in a struct in a class constructor?
    I want to create a 11x11 vector.

    Say I have the struct:
    Code:
    struct tree_node
    {
        int x, y;
        int visited;
        int UCT_value;
        double percentage;
        tree_node *parent;
    }
    Here is where it is declared as a 2d array:
    Code:
    vector<vector<tree_node*> > board_values
    Here is what I attempted to do to initialize two things:
    Code:
    board_values(10, vector<tree_node*>(11, tree_node->x  // and tree_node->y
    and I got stuck on how to initialize it. Would I have to manually malloc/new, set the fields to zero manually for each, then push it on the vector 121 times?
    Last edited by edishuman; 03-12-2012 at 06:39 PM.

  2. #2
    Registered Boozer
    Join Date
    Jan 2008
    Location
    Canada
    Posts
    1,742
    Couldn't you do something like this:
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    struct tree_node {
        int x, y;
        int visited;
        int UCT_value;
        double percentage;
        tree_node *parent;
    
        tree_node()
        : x(0), y(0), visited(0), UCT_value(0), percentage(0.0), parent(0) { }
    };
    
    int main() {
        vector<vector<tree_node*> > board_values(11);
        for (int rows = 0; rows < 11; rows++)
            for (int cols = 0; cols < 11; cols++)
                board_values[rows].push_back(new tree_node);
    }
    Last edited by oogabooga; 03-12-2012 at 08:30 PM.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,515
    Why don't you suggest an approach instead of posting an answer?
    Elysia likes this.
    Arrogance breeds bad code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initializing array of structs
    By Nerigazh in forum C Programming
    Replies: 3
    Last Post: 11-19-2007, 11:26 PM
  2. Unresolved external on vector inside class constructor
    By Mario F. in forum C++ Programming
    Replies: 13
    Last Post: 06-20-2006, 12:44 PM
  3. std::vector resize and Class copy constructor
    By pianorain in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2005, 12:52 PM
  4. trouble initializing constructor
    By dantestwin in forum C++ Programming
    Replies: 11
    Last Post: 07-06-2004, 01:31 AM
  5. Initializing array and non-standard constructor
    By Jasel in forum C++ Programming
    Replies: 15
    Last Post: 11-10-2003, 01:56 PM

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