Questions about vector::resize

I'm trying to initialize a private vector in a constructor. When I call resize before I push_back on the vector, I get a bunch of 0s when I call GameBoard::print(). When I move resize after I push_back the vector, GameBoard::print() outputs what I would expect. Can someone explain why it's doing that? And, do I need to even bother resizing vectors at all? (My goal is to store a great many of these vectors in a hashset for AI purposes).

Applicable code is below.

Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
///////////////////////////////
GameBoard.h

#include <vector>
#include <iostream>
#include <math.h>

class GameBoard {

public:
	//constructors
	GameBoard(int boardSize);
	~GameBoard();

	//member functions
	void print();

private:
	//member data
	int m_boardSize;
	int m_rowColSize;
	std::vector<int> m_board;
};

////////////////////////////////
GameBoard.cpp

#include "GameBoard.h"

GameBoard::GameBoard( int boardSize ) {
	m_board.resize( boardSize );
	for ( int i = 0; i < boardSize; i++ ) {
		m_board.push_back( i );
	}
	m_boardSize = boardSize;
	m_rowColSize = std::sqrt( double( boardSize) );
}

void GameBoard::print() {
	const int MAX_ROWS = m_rowColSize;
	const int MAX_COLS = m_rowColSize;
	for ( int rows = 0; rows < MAX_ROWS; rows++ ) {
		for ( int cols = 0; cols < MAX_COLS; cols++ ) {
			std::cout << m_board[ ( rows * MAX_ROWS ) + cols ];
		}
		std::cout << "\n";
	}
}

/////////////////////////////
main.cpp
#include "GameBoard.h"

using namespace std;

int main() {
	GameBoard gb(9);
	gb.print();
	return 0;
}
When you resize a vector to a size that is larger than it's current size every new space created is assigned to a 0 to help prevent errors, when resizing you have the option of assigning these newly created spaces to something else using vec.resize(<size>, <assignment>);

vector's function member push_back(<val>) will increase the vectors storage size by +1 to add the new element...

So when you call resize() it'll setup the vector of your chosen size and set them all to 0's then when when you call push_back it does still add more values to the end of your vector, these are simply not shown because they're stored in vector position exceeding your boardsize because...

When you call push_back() first your size increase by +1 each time and the value given as the argument is assigned. Then when you call resize() nothing actually happens because your vector is already this size...

Hope this helped, If not then look at the vector reference for
push_back() http://www.cplusplus.com/reference/vector/vector/push_back/
resize() http://www.cplusplus.com/reference/vector/vector/resize/
Last edited on
resize() for vector means that n values (with default values i.e. default constructor) exists afterwards.

On line 33 you add values to those already present. So resize() is wrong in this case.

http://www.cplusplus.com/reference/vector/vector/resize/
> And, do I need to even bother resizing vectors at all?

In general, no. vectors resize themselves as you add or remove elements.

You could call std::vector::reserve() when the number of elements is known beforehand.

1
2
3
4
5
6
GameBoard::GameBoard( int boardSize ) 
{
	m_board.reserve( boardSize );
	for ( int i = 0; i < boardSize; i++ ) m_board.push_back( i );
	// ...
}


http://en.cppreference.com/w/cpp/container/vector
Thank you all for helping to clarify how push_back and resize work. All replies helped a bunch.
No problem pal (or at least no problem from me), That's what the forums are here for, I'm glad to say you had a well structured question with all the information we needed without just chucking in all the unnecessary extras, (thanks)...
Makes it easier for us to help (and more willing if things are nice and clear)
Topic archived. No new replies allowed.