Hello everyone,
I need to make a constructor that initialize a 2d vector array by zeros or nulls......
how can i do that ._.
tried nested loops like in 2d arrays but failed
should i use push_back?or is there a way i can do this without it?
Printable View
Hello everyone,
I need to make a constructor that initialize a 2d vector array by zeros or nulls......
how can i do that ._.
tried nested loops like in 2d arrays but failed
should i use push_back?or is there a way i can do this without it?
What have you tried?
Soma
Hello, as phantomotap says: tried anything? Show the code.
std::vector can be initialized just when you are creating it, like this:
If you define a vector like this (with no default value):Code://Creates 2D vector of size 10 and the 0 in the line below defines the default size.
std::vector<std::vector <int>> Storage(10, std::vector<int>(10, 0));
std::vector<std::vector <int>> Storage(10, std::vector<int>(10));
Then initialize it through a loop with another inner loop:
Is this all you are trying to do? You can use push.back if you want.Code:int initvalue = 0;
for(int i = 0; i < 10; i++)
{
for(int ii =0; ii < 10; ii++)
{
Storage[ii][i] = initvalue;
}
}
Or are you trying to create a 2D vector by yourself, something along these lines:
Code:class Vec2D
{
private:
int v[2];
public:
Vec2D(int x, int y);
//etc...
}
thx alot on the replies
this is the last thing i did and it doesn't show any errors anymore but i don't know is this even correct or not -_-" "new to programming"
the 2d vector in protected area is useless now right? -_-"Code:protected:
char w;
int x,y;
int i,j;
vector<vector<char> > Vec(int i, vector<char>(int i,int j));
public:
rand_m_tst()
{
w='$';
x=0;
y=0;
i=20;
j=0;
vector<vector<char>> Vec(i,vector<char>(i,j));
for(int f=0;f<20;f++)
{
for(int j=0;j<20;j++)
Vec[f][j]=NULL;
}
};