// Initialisation.
for(size_t i=0;i<m.size1();++i)
{
m.at_element(i, 0).first = static_cast<float>(i)*(-1.f);
}
for(size_t j=1;j<m.size2();++j)
{
m.at_element(0, j).first = static_cast<float>(j)*(-1.f);
}
// Initialisation.
for(size_t i=0;i<m.size1();++i)
{
m.at_element(i, 0).first = static_cast<float>(i)*(-1.f);
}
for(size_t j=1;j<m.size2();++j)
{
m.at_element(0, j).first = static_cast<float>(j)*(-1.f);
}
To copy to clipboard, switch view to plain text mode
Even though this is the wrong place to answer a C++ / STL question...
Do you know what this matrix looks like after this initialization step? I do not know if the first index ("i") in at_element( i, j ) refers to row or column, so let's just assume it is the column index. It doesn't matter for this answer - the matrix is just transposed if i is the row index.
By the way, the std:: pair<double, Direction> constructor will put garbage into the elements of the pair. It will not initialize the double to zero or "Direction" to whatever it should hold by default.
And why do you use static_cast<float> when your Cell pair's "first" member is a double?
Your matrix is small enough that you can type it out:
Length of "ABABA" is 5 and length of "BBBBBAAA" is 8, so 5 columns and 8 rows.
Your first initialization loop goes from i = 0 - 4, and sets the double part of Cell to -i for all cells m[i, 0].
The second loop goes from j = 1 - 7 and sets the double to -j for all cells m[0, j].
The remainder of the cells are undefined.
i = 0 1 2 3 4
j = 0 0 -1 -2 -3 -4
j = 1 -1 ? ? ? ?
j = 2 -2 ? ? ? ?
j = 3 -3 ? ? ? ?
...
j = 7 -7 ? ? ? ?
Length of "ABABA" is 5 and length of "BBBBBAAA" is 8, so 5 columns and 8 rows.
Your first initialization loop goes from i = 0 - 4, and sets the double part of Cell to -i for all cells m[i, 0].
The second loop goes from j = 1 - 7 and sets the double to -j for all cells m[0, j].
The remainder of the cells are undefined.
i = 0 1 2 3 4
j = 0 0 -1 -2 -3 -4
j = 1 -1 ? ? ? ?
j = 2 -2 ? ? ? ?
j = 3 -3 ? ? ? ?
...
j = 7 -7 ? ? ? ?
To copy to clipboard, switch view to plain text mode
Then, in your nested loops starting in line 17, you start with i = 1 and j = 1, and access m[i-1, j-1]. For the first access m[0,0], this is in fact zero. For the next one m[0,1], you get -1.
So if this isn't what you intended to do when you wrote this code, you need to sit down with a pencil and a piece of paper and write out what you think your matrix should look like, and figure out why your code doesn't do that.
But don't ask here. Do as Amleto said and move your discussion to the general programming section.
Bookmarks