How to put NULL in all cells of a matrix vector? - c++

i'm trying to initialize all cells of a matrix with NULL values, but something is wrong here.
the code :
vector<vector<Distance*> > distanceMatrix;
for (int i = 0; i < 7 ; i++)
for (int j = 0; j < 7 ; j++)
distanceMatrix[i][j].push_back(NULL);
i bet it's something stupid, thanks for the help.

From the std::vector reference page:
Vectors can be constructed with some values in them.
You may try:
vector<vector<Distance*> > distanceMatrix(7, vector<Distance*>(7, NULL));
Also, regarding your problem:
vector<vector<Distance*> > distanceMatrix;
for (int i = 0; i < 7 ; i++)
for (int j = 0; j < 7 ; j++)
distanceMatrix[i][j].push_back(NULL); //1
When you code first reach //1, distanceMatrix[i] resolves to distanceMatrix[0] but you did not call distanceMatrix.push_back(vector<Distance*>()) so you are referring to a non initialized cell.
To correct code would have been:
vector<Distance*> vec;
for (int j = 0; j < 7 ; j++)
vec.push_back(NULL);
vector<vector<Distance*> > distanceMatrix;
for (int i = 0; i < 7 ; i++)
{
distanceMatrix.push_back(vec);
}
Which is still far worse than my first suggestion.

Since the matrix is empty to begin with, you need to push_back every value and every row, not depending on its position in the matrix:
vector<Distance*> row;
for(int j=0; j < 7; j++)
row.push_back(NULL);
vector<vector<Distance*> > distanceMatrix;
for(int i=0; i < 7; i++)
distanceMatrix.push_back(row);

Related

Iterate over vectors with n and n*n elements

I have this Google test assertions
ASSERT_FLOAT_EQ(longerVector[0], shorterVector[0]);
ASSERT_FLOAT_EQ(longerVector[1], shorterVector[1]);
ASSERT_FLOAT_EQ(longerVector[2], shorterVector[2]);
ASSERT_FLOAT_EQ(longerVector[3], shorterVector[0]);
ASSERT_FLOAT_EQ(longerVector[4], shorterVector[1]);
ASSERT_FLOAT_EQ(longerVector[5], shorterVector[2]);
ASSERT_FLOAT_EQ(longerVector[6], shorterVector[0]);
ASSERT_FLOAT_EQ(longerVector[7], shorterVector[1]);
ASSERT_FLOAT_EQ(longerVector[8], shorterVector[2]);
as you can see, there are two vectors, shorter with n elements and longer with n*n elements (in this case 3 and 9 respectively).
How could I create two nested for loops, which would make my code shorter and simpler?
I tried for example this but it failed. I understand why but I was not able to come up with anything better.
for(size_t i = 0; i < shorterVector.size(); i++)
{
for(size_t j = 0; j < shorterVector.size(); j++)
{
ASSERT_FLOAT_EQ(longerVector(i*j), shorterVector(j);
}
}
Thank you very much for any help and I apologize for maybe a dumb question.
Use this instead
for(size_t i = 0; i < longerVector.size(); i++)
{
ASSERT_FLOAT_EQ(longerVector[i], shorterVector[i%3]);
}
for(size_t i = 0; i < shorterVector.size(); i++)
{
for(size_t j = 0; j < shorterVector.size(); j++)
{
ASSERT_FLOAT_EQ(longerVector[i*shorterVector.size() + j], shorterVector[j]);
}
}
You should assert before that shorterVector.size() * shorterVector.size() == longerVector.size().
If you do not want to have the multiplication in the inner loop body you can do:
for(size_t i = 0, k = 0; i < shorterVector.size(); i++)
{
for(size_t j = 0; j < shorterVector.size(); j++)
{
ASSERT_FLOAT_EQ(longerVector[k++], shorterVector[j]);
}
}

Which is better, a single for loop or double for loop to iterate through 2-D array? C++

Let's say that I have a 9-by-9 2-D array. Is there a difference between looping through with a single loop or multiple loops?
for (int i = 0; i < 81; i++)
currentEdit[i / 9][i % 9] = 0;
VS.
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
currentEdit[i][j] = 0;
The right choice is multiple loops. Keep in mind that it will perform much less operations since it does not have to divide or calculate the module to access the array position.
This is the right choice:
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
currentEdit[i][j] = 0;

Multi Dimensional vectors

Require a 2D-vector with a pair(int, int) as elements.The following code gives SIGSEGV on running.How can it be resolved ?
int main()
{
vector< vector<pair<int, int> > >v;
//vector< vector<pair<int, int> > >v(3), problem is resolved, but how ?
for(int i = 0; i < 3; ++i)
for(int j = 0; j < 3; ++j)
v[i].push_back(make_pair(i, j));
for(int i = 0; i < 3; ++i)
{
cout<<"\n";
for(int j = 0; j < 3; ++j)
cout<<"{"<<v[i][j].first<<", "<<v[i][j].second<<"} ";
}
return 0;
}
In the beginning, v contains solely nothing, so the SIGSEGV if received at
v[0].push_back(make_pair(0, 0)); // First loop
If you initialize v with a length of 3, then v[0] is a valid statement and won't cause a segmentation fault.
The following code should work if you don't initialize v with a size.
for(int i = 0; i < 3; ++i){
vector<pair<int,int> > t;
for(int j = 0; j < 3; ++j)
t.push_back(make_pair(i, j));
v.push_back(std::move(t));
}
Thanks to Zereges for code improvement
You are inserting wrong.
for(int i = 0; i < 3; ++i)
for(int j = 0; j < 3; ++j)
v[i].push_back(make_pair(i, j));
Change your code of insertion like below to work properly:
for(int i = 0; i < 3; ++i)
{
vector<pair<int, int>> vctr;
for(int j = 0; j < 3; ++j)
{
vctr.push_back(make_pair(i, j));
}
v.push_back(vctr);
}
create a vector of pair, lets say: (vctr), then insert pair<i,j> in (vctr).
and then insert vctr to vector (v).

Copying values of 2D array into another

I have a 2D array that I want to assign the values of another array. I'm making a game of life simulator and have everything else working but this. My code is this:
for(int i = 0; i < ROWS; i++) {
for (int j = 0; i < COLS; j++) {
current[i][j] = next[i][j];
}
}
current and next are both bool's. I keep getting the error code EXC_BAD_ACCESS in X-Code. I'm unsure what I'm doing wrong
To re-iterate my comment, the innerloop compares i with COL rather than j. So
for(int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
current[i][j] = next[i][j];
}
}
should solve the issue

Setting multidimentional arrays as vector in C++

I have "int array[10][10]" (Two dimentional (Size can be changed!) )
and I want to set all of items into "vector> Vector"
I tried:
vector<vector<int>> Vector;
for(int i = 0; i < 10; i++)
for(int j = 0; j < 10; j++)
{
Vector[i][j] = array[i][j];
}
But it doesnt work. I get this exception:
The program has unexpectedly finished.
vector<vector<int>> Vector(10, vector<int>(10));
will do the trick