c++ - How to print a two dimensional array in cocos2dx -


i have two-dimensional array want print output of visual studio see result each time modify that, tried using std::cout , not work, if use cclog function automatically write newline each time it's called , it's not two-dimensional array pretty solution, tried cclog not sure what's difference cclog time give compiling error :(

like want output be:

1,2,4,4,5 5,5,4,3,0 4,4,4,4,7 6,6,6,6,6 

here tried:

void helloworld::printbrickarray() {     cclog("will print brick array");     std::cout << "===begin of array ====" << std::endl;     (int = 0; < matrix_y; i++) {         (int j = 0; j < matrix_x; j++) {             //cclog("%d", this->brickarray[i][j]);             std::cout << this->brickarray[i][j] << ' ';         }         std::cout << std::endl;     }     std::cout << "*****end of array *****" << std::endl;     std::cout.flush(); } 

how coco2dx?

cclog or cocos2d::log uses visual studio's debug windows, different write console std::cout works.

therefore, there 2 ways rid of problem: writing console using std::cout or writing output windows using different methods cclog

first choice, have change project type win32 application project win32 console project. kind of going around visual studio stuffs, , in cases, project created automatically via cocos2d's console. can see this post. i'm not recommend way imo.

second choice, use own code write output discussed here.

there way can use std::string , std::ostringstream "print" variables buffer, , print string output windows via cclog

cclog little bit wrapping code make convenience log resources checking, errors, file processing, etc occur when run time. if not in cases, should set break points view values instead.

edited: since chose second approach, recommend using std::ostringstream sprintf, , using cclog instead of outputdebugstring (because print out , independent os, no need arguments)

here example code:

#include <vector> #include <sstream> // ostringstream #include <windows.h> // outputdebugstringa using namespace std;  int main(void) {     // assuming have 2d array     vector< vector<int> > arr2d;     arr2d.push_back({ 2,2,1,4 });     arr2d.push_back({ 2,4,1,5 });     arr2d.push_back({ 2,4,7,2 });     arr2d.push_back({ 3,2,0,1 });      ostringstream buffer;     (int = 0; < arr2d.size(); i++)     {         (int j = 0; j < arr2d[i].size(); j++)         {             buffer << arr2d[i][j] << '\t';         }         buffer << endl;     }      // assuming use outputdebugstring windows-only     //outputdebugstringa(buffer.str().c_str());      // recommend     cocos2d::log(buffer.str().c_str());      return 0; } 

now, buffer works same cout, "print" buffer instead, can 1 using str(). cocos2d::log use c-style string, c_str() rid of problem

see more std::ostringstream here


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -