c++ - Initializing a vector from arrays going wrong -


i'm creating vector<vector<char>> char arrays 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, expected see printed board looking original char arrays. however, it's printing this:

x x x x x x x x x x - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - x x x x x x x x x x  

where going wrong?

edit: here's how i'm printing board:

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

your indices wrong. end iterator supposed point 1 past end of array (and supposed pointer, not value have currently). using std::begin , std::end make sure don't 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, (and should) construct in place c++11 using initializer lists - it'll save lot of boilerplate code.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -