开发者

Iterating over 2D vector of "char" in C++, blank characters are not printing out

开发者 https://www.devze.com 2023-02-14 20:54 出处:网络
I am currently working on a battleship text based game and I am switching my container that I am using to store the board from a 2D array of char to a 2D vector. In the code snipped below I am initial

I am currently working on a battleship text based game and I am switching my container that I am using to store the board from a 2D array of char to a 2D vector. In the code snipped below I am initializing the entire board and setting all the characters in it to be blank spaces to begin with. What follows this is all of my code to create the board etc.

const int width  = 100;
const int height = 35;
vector< vector<char> > buffer(width, vector<char>(height,0));

for (int y = 0; y < height; ++y)
    for (int x = 0; x < width; ++x)
        buffer[x][y] = ' ';

When I am going to output the board to the screen I am trying to use the iterators that are provided for vectors. Only problem I am having is that when using the iterator it seems to ignore blank spaces in my vector so my game board does not look as it should. Just using a double for loop to iterate through the vector then the output is fine.

vector<vector<char> >::const_iterator row;
vector<char>::const_iterator col;
for (row = buffer.begin(); row != buffer.end(); row++) {
    for (col = row->begin(); col != row->end(); col++) {
            cout << *col;
    }
    cout &l开发者_如何转开发t;< endl;
}

This is the first time im attempting to use vectors so im stumped. Anyone know why it would be ignoring the blank characters?


My first question is: "why are you using vectors for a simple 2-D array?" I would simply use a two-dimensional array and be done with it. An efficient way to allocate a 2-D array of objects with a single malloc() call (so it can be freed with a single free() call) is:

/* set up the memory for a 2D matrix with entries of size "size" */
void** matrix2D(long rows, long columns, long size)
{
    long    i;
    unsigned long long      row_size = (unsigned long long)columns * (unsigned long long)size;
    unsigned long long      data_size = ((unsigned long long)rows * (unsigned long long)columns + 1) * (unsigned long long)size;
    unsigned long long      pointer_size = (unsigned long long)rows * sizeof(void*);
    void**  result;

    if ( (result = (void**)malloc((size_t)(data_size + pointer_size))) == NULL ) {
            return NULL;
    }

    // take the first bit for a vector pointing to the m_pData for each row
    char* pdata = (char*)result + pointer_size;
    if ((unsigned long)pdata % size) {
      pdata += size - (unsigned long)pdata % size;
    }

    // for each row, set up the pointer to its m_pData
    for (i = 0; i < rows; i++) {
            result[i] = (void*)pdata;
            pdata += row_size;
    }

    return result;
}

I would then setup your matrix using:

char** buffer = (char**)matrix2D(height, width, sizeof(char));

I would initialize the array using:

for (int i = 0; i < height; ++i)
    for (int j = 0; j < width; ++j)
        buffer[i][j] = ' ';

and I would print the array using:

for (int i = 0; i < height; ++i) {
    for (int j = 0; j < width; ++j)
        cout << buffer[i][j];
    cout << endl;
}


You don't need to use the vector<vector<char> >::iterator. The vector class has overloaded for you the subscript operator[]. So you can write:

for(size_t i = 0; i < height; i++)
{
    for(size_t j = 0; j < width; j++)
    {
        cout << buffer[i][j]; // buffer is a vector<vector<char> >
    }
    cout << "\n";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消