Thread: Vector question

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    115

    Vector question

    Hello, I have created a vector of pointers.
    If I resize my vector, will it be filled in with NULL values?
    Or will it point to garbage? If it will point to garbage, how do I init them on NULL?

    Code:
    vector< vector< Tile*> > board;
    
    board.resize(size);
    
    for (int i = 0; i < size; i++)
         board[i].resize(size);
    This does not work because I am pointing to the values:

    Code:
         for(int i =0; i < size; i++)
         {
             for(int j=0; j < size; j++)
             {
                 board[i][j] = NULL;
             }
         }
    Thank you

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    push_back

    You could try using the push_back() method, and I know that int vectors are set to default value of 0, so maybe?

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You should be supplying the other argument to resize()

    board[i].resize(size, NULL);

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I believe that if you resize a vector, all of the values will be default-constructed, so that would mean NULL, yes.
    However, whiteflags has a point. You shouldn't be using resize anyway, unless you have a very specific requirement. Use push_back, and if possible, don't use pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed