// single
vector<int> chart(16, 55);
vector<int>::iterator ChartEnd = chart.end();
for (vector<int>::iterator i = chart.begin(); i != ChartEnd; i++)
cout << *i << endl;
vector array 16 x 16
I created a little console board game 16 x 16 with double array's
Mgame MainArray[ArraySize][ArraySize];
I want to make a similar game but using (STL) vectors
How can I use double array style with vectors, and how would I
iterate through them?
creating and using nested vectors:
filling that to be 16x16 is annoying.
it may be easier to do:
-me
std::vector< std::vector<int> > myVector;std::vector< std::vector<int> >::iterator it = myVector.begin();std::vector< std::vector<int> >::iterator ite = myVector.end();for( ; it != ite; ++it ){ std::vector<int>::iterator jt = (*it).begin(); std::vector<int>::iterator jte = (*it).end(); for( ; jt != jte; ++jt ) { int anInt = *jt; }}
filling that to be 16x16 is annoying.
it may be easier to do:
//create and fill your vector with zerosstd::vector<int> myVector;myVector.reserve(16*16);std::fill_n(myVector.begin(), 16*16, 0);//retrieve dataint getXY( int x, int y ){ assert( x < 16 && y < 16 ); return myVector[ x + y*16 ];}//iterate:std::vector<int>::iterator ite = myVector.end();for( std::vector<int>::iterator it = myVector.begin(); it != ite; ++it ){ int anInt = *it;}
-me
I'm having trouble with the default constructor, I've tried a few different things all of them crashing the program. Im assigning int's to the vector.
My prefered choise would be "fill_n( )" its not working either.
My prefered choise would be "fill_n( )" its not working either.
#include <vector>#include <iterator>#include "Functions.h"using namespace std;class Sgame{private: vector<int> Map; vector<int> Ghost;public: // default constructor Sgame(){ // Map(256, 99); // no good // wanted to use "fill_n" but it made the program crash /*Map.reserve(ArraySize * ArraySize + ArraySize); // try'd to add extra space fill_n(Map.begin(),ArraySize * ArraySize, 33);*/ /*Ghost.reserve(ArraySize * ArraySize); fill_n(Ghost.begin(), (Ghost.end() + ArraySize * ArraySize), 1);*/ // crash program int x = 0; int y = 0; // turns out this crashes the program too //Ghost.reserve(ArraySize * ArraySize); //vector<int>::iterator Gend = Ghost.end() + (ArraySize * ArraySize); //for(vector<int>::iterator i = Ghost.begin(); i != Gend; i++, x++) // if(x == ArraySize){ // x = 0; // y++; // } // Set_GhostXY(99, x, y); // not sure how to pass along iterators so i use int's }/////////////////////////////// end default constructor // get infoint Get_MapXY(const int &x, const int &y){ if (x < ArraySize && y < ArraySize) return Map[x + y * ArraySize];}int Get_GhostXY(const int &x, const int &y){ if (x < ArraySize && y < ArraySize) return Ghost[x + y * ArraySize];} // set infovoid Set_MapXY(const int A, const int &x, const int &y){ Map[x + y * ArraySize] = A;}void Set_GhostXY(const int A, const int &x, const int &y){ Ghost[x + y * ArraySize] = A;} // print chartvoid PrintGhost(){vector<int>::iterator Cend = Ghost.end(); int y = 0; int x = 0; for (vector<int>::iterator Cbegin = Ghost.begin(); Cbegin != Cend; Cbegin++, x++){ if(x == ArraySize){ y++; cout << endl; x = 0;} //if(x == (ArraySize - 1) || y == 0 || x == 0 || y == (ArraySize - 1)){// set the borders of the board //*Cbegin = 11; //} cout << *Cbegin << " "; } cout << endl;}};
reserve only sets aside a chunk of memory for future use. You're not allowed to use that memory until/unless the vector is resized or otherwise grown to include it.
Σnigma
Sgame() : // initialisation in initialiser list, the best way Map(ArraySize * ArraySize, 33){ // using the assign member function Map.assign(ArraySize * ArraySize, 33); // using resize // this only sets new elements to the given value, // so can only be used here is the Map is empty Map.resize(ArraySize * ArraySize, 33); // using fill_n // if the map is already large enough there is no need to resize Map.resize(ArraySize * ArraySize); fill_n(Map.begin(), ArraySize * ArraySize, 33); // using fill // if the map is already large enough there is no need to resize Map.resize(ArraySize * ArraySize); fill_n(Map.begin(), Map.end(), 33);}
Σnigma
wow didnt know you could initialize outside the brackets.... thanks works great
// default constructor
Sgame() :
Map((ArraySize * ArraySize), 99), Ghost((ArraySize * ArraySize), 33)
{
}
// default constructor
Sgame() :
Map((ArraySize * ArraySize), 99), Ghost((ArraySize * ArraySize), 33)
{
}
Quote:Original post by pascalosti
wow didnt know you could initialize outside the brackets.... thanks works great
Its called an initialization list.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement