user2356560
user2356560

Reputation:

Initializing a vector from arrays going wrong

I'm creating a vector<vector<char>> from char arrays as follows:

typedef vector<vector<char>> board;
...
char row0[] = {'X', '-', '-', '-', '-', '-', '-', '-', '-', 'X'};
char row1[] = {'-', 'X', '-', '-', '-', '-', '-', '-', 'X', '-'};
char row2[] = {'-', '-', 'X', '-', '-', '-', '-', 'X', '-', '-'};
char row3[] = {'-', '-', '-', 'X', '-', '-', 'X', '-', '-', '-'};
char row4[] = {'-', '-', '-', '-', 'X', 'X', '-', '-', '-', '-'};
char row5[] = {'-', '-', '-', '-', 'X', 'X', '-', '-', '-', '-'};
char row6[] = {'-', '-', '-', 'X', '-', '-', 'X', '-', '-', '-'};
char row7[] = {'-', '-', 'X', '-', '-', '-', '-', 'X', '-', '-'};
char row8[] = {'-', 'X', '-', '-', '-', '-', '-', '-', 'X', '-'};
char row9[] = {'X', '-', '-', '-', '-', '-', '-', '-', '-', 'X'};

vector<char> v0(row0[0], row0[9]);
vector<char> v1(row1[0], row1[9]);
vector<char> v2(row2[0], row2[9]);
vector<char> v3(row3[0], row3[9]);
vector<char> v4(row4[0], row4[9]);
vector<char> v5(row5[0], row5[9]);
vector<char> v6(row6[0], row6[9]);
vector<char> v7(row7[0], row7[9]);
vector<char> v8(row8[0], row8[9]);
vector<char> v9(row9[0], row9[9]);

board test;
test.push_back(v0);
test.push_back(v1);
test.push_back(v2);
test.push_back(v3);
test.push_back(v4);
test.push_back(v5);
test.push_back(v6);
test.push_back(v7);
test.push_back(v8);
test.push_back(v9);

Having done so, I expected to see the printed board looking like the original char arrays. However, it's printing like this:

X X X X X X X X X X
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
X X X X X X X X X X 

Where am I going wrong?

EDIT: Here's how I'm printing the board:

    void printBoard(Board b){
    for (int r = 0; r <10; r++){
    cout << "\n";
        for (int c = 0; c <10; c++){
        cout <<  b[r][c] << " ";
        }
}

Upvotes: 0

Views: 152

Answers (3)

Yuushi
Yuushi

Reputation: 26040

Your indices are wrong. The end iterator is supposed to point to one past the end of the array (and it is supposed to be a pointer, not a value as you have it currently). Using std::begin and std::end will make sure you don't get this wrong.

#include <vector>
#include <iostream>
#include <iterator>

using namespace std;

typedef vector<vector<char>> board;

void print_board(const board& b)
{
    for(const auto& inner : b) {
        for(auto c : inner) {
            std::cout << c << " ";
        }
        std::cout << "\n";
    }
}

int main() 
{
    char row0[] = {'X', '-', '-', '-', '-', '-', '-', '-', '-', 'X'};
    char row1[] = {'-', 'X', '-', '-', '-', '-', '-', '-', 'X', '-'};
    char row2[] = {'-', '-', 'X', '-', '-', '-', '-', 'X', '-', '-'};
    char row3[] = {'-', '-', '-', 'X', '-', '-', 'X', '-', '-', '-'};
    char row4[] = {'-', '-', '-', '-', 'X', 'X', '-', '-', '-', '-'};
    char row5[] = {'-', '-', '-', '-', 'X', 'X', '-', '-', '-', '-'};
    char row6[] = {'-', '-', '-', 'X', '-', '-', 'X', '-', '-', '-'};
    char row7[] = {'-', '-', 'X', '-', '-', '-', '-', 'X', '-', '-'};
    char row8[] = {'-', 'X', '-', '-', '-', '-', '-', '-', 'X', '-'};
    char row9[] = {'X', '-', '-', '-', '-', '-', '-', '-', '-', 'X'};

    vector<char> v0(std::begin(row0), std::end(row0));
    vector<char> v1(std::begin(row1), std::end(row1));
    vector<char> v2(std::begin(row2), std::end(row2));
    vector<char> v3(std::begin(row3), std::end(row3));
    vector<char> v4(std::begin(row4), std::end(row4));
    vector<char> v5(std::begin(row5), std::end(row5));
    vector<char> v6(std::begin(row6), std::end(row6));
    vector<char> v7(std::begin(row7), std::end(row7));
    vector<char> v8(std::begin(row8), std::end(row8));
    vector<char> v9(std::begin(row9), std::end(row9));

    board test;
    test.push_back(v0);
    test.push_back(v1);
    test.push_back(v2);
    test.push_back(v3);
    test.push_back(v4);
    test.push_back(v5);
    test.push_back(v6);
    test.push_back(v7);
    test.push_back(v8);
    test.push_back(v9);

    print_board(test);
}

Of course, you could (and should) construct this in place with C++11 using initializer lists - it'll save a lot of boilerplate code.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409136

There are a few ways to pushing the data into the vectors. The first, and IMHO the best way, is to use the C++11 uniform initialization:

std::vector<char> v0 = {'X', '-', '-', '-', '-', '-', '-', '-', '-', 'X'};
// etc.

The second way is also using another C++11 feature: std::begin and std::end:

std::vector<char> v0(std::begin(row0), std::end(row0));

A third way, and the way to handle "iterators" of arrays before C++11 is to use pointers to the array:

std::vector<char> v0(static_cast<char*>(row0), static_cast<char*>(row0 + 10));

Note that I added 10 to the end, this is because the last iterator is actually one beyond the last entry.


By the way, if you have uniform location, you can initialize the board that way too:

board test = { v0, v2, /* etc... */ };

Upvotes: 0

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70931

To initialize a vector from array you should write something like:

vector<char> v0(row0, row0 + 10);

As opposed to what you have done. I am surprised this code even compiles. Probably you should play a bit with compiler options to print more warnings.

Upvotes: 1

Related Questions