c++ - Loop help: string vector to 3D char vector -


i'm new c++ , first attempt @ 3d vector. i'm trying take input file of variable length this:

xxooo##xx xoxxxoxoo xxx#oxoo# oxxxoxoox xxoooo#xx xxxo#o### xxo#o#xxo x##oxxoox xxx##oxoo xoxx#xooo 

and turn 3d char vector each line 3x3 box first 3 characters being first row, next 3 2nd row, , last 3 3rd row. example first row of input should turn this:

x x o o o # # x x 

this attempt @ solution, feel i've made several mistakes:

vector<vector<vector<char> > > makeboard(vector<string> iflines) {// function fill game boards input strings  vector<vector<vector<char> > > charboard;  (int = 0; != iflines.size(); i++)  {        (int j = 0; j < 9; j=j+3)     {         charboard[i][j/3][0] = iflines[i][j];          charboard[i][j/3][1] = iflines[i][j+1];         charboard[i][j/3][2] = iflines[i][j+2];          }    }     return charboard; } 

would please me out here?

edit: edited suggestions plus couple things tried fix. error: segmentation fault: 11. when try run it.

you calling push_back on char. need do

charboard[i][j/3][k] = iflines[i][j] 

since working fixed 3x3 array don't need use std::vector inner components. std:array suffice, like

using gameboard = std::array<std::array<char, 3>, 3>; vector<gameboard> charboard; 

in addition don't need use if/else compute k, there modulo % operator yields remainder of division it's fine situation:

charboard[i][j/3][j%3] = iflines[i][j] 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -