char vector question

Jun 9, 2013 at 12:16am
Hello, i just had question but did not understand the previous answers

I want to save the char[8][8] // fixed size 2 dimension array

to a vector

such as

vector<?????> temp;

is there anyway to approach this?
Jun 9, 2013 at 3:29am
You mean you're trying to do a 2D vector?

vector<vector<char> > temp;

This should make the 2D vector array
Jun 9, 2013 at 3:38am
I want to make vector for
char[8][8]

vector<char[8][8]....> temp

something like this

8.8 is fixed
Jun 9, 2013 at 10:54pm
When you do vector<vector<char> > temp; , it makes a 2d char vector. So temp[0][0] would be a slot, temp[n][n] would be another, etc...
Jun 9, 2013 at 11:55pm
When you do vector<vector<char> > temp; , it makes a 2d char vector. So temp[0][0] would be a slot, temp[n][n] would be another, etc...


vector<vector<char> > temp; makes an empty vector of empty vectors. Using temp[x][y] will try to access an element that doesn't exist unless the vectors are resized (or perhaps initialized correctly.)

vector<vector<char>> temp(8, vector<char>(8)) ;
creates a vector of 8 vectors of 8 char which may be used (almost) equivalently to:
char[8][8] temp ;

Alternately,
1
2
3
4
5
6
vector<vector<char>> temp ;
temp.resize(8) ;
for ( auto& element : temp )
    element.resize(8) ;

// now temp may be indexed appropriately. 

Last edited on Jun 9, 2013 at 11:56pm
Topic archived. No new replies allowed.